-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
belongs_to should default to required: true #18233
Comments
I can take this, but I'm afraid this can cause a lot of harm. |
Rocking. I think the main issue will be thinking about whether this has any backwards incompatibility issues. Like if you upgrade an app that already declares validates_presence_of or something. I don’t think it should be a problem, since the validation should just overwrite in that case, I believe. But something to be on the lookout for. 👍
|
My fears were fulfilled :( For this naive implementation (actually it should be changed somewhere in the reflection): diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 954ea38..e0edc28 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -23,6 +23,11 @@ module ActiveRecord::Associations::Builder
add_counter_cache_methods mixin
end
+ def self.define_validations(model, reflection)
+ reflection.options[:required] ||= true
+ super
+ end
+
private
def self.add_counter_cache_methods(mixin) I'm getting (because unsaved objects) a lot failing tests:
Since
it is not good idea to add |
@simi - the implementation given there is incorrect, since If we make this the default, we should include a warning that it alone isn't sufficient to maintain referential integrity - it suffers from the same sort of race condition as
I'm not personally a huge fan of options where the default is |
@al2o3cr sure, this is really bad implementation. But since there is no usage of
|
I like On Dec 29, 2014, at 5:32, Josef Šimánek [email protected] wrote:
|
Matt, this is to give you the best error message in the majority of the cases. You’ll still need to back it up with a unique constraint in the DB to prevent what you mention. But such an error would raise an exception, not a validation error. So the less that needs to happen, the better. On Dec 29, 2014, at 5:23, Matt Jones [email protected] wrote:
|
I think it will be better to introduce new relation based on |
Since we're dropping support for Ruby 1.9, why not just use keyword arguments to set the default? |
This would also constitute a major breaking change for most apps, so we should probably have a deprecation cycle. |
We would need to pair this with a corresponding change in migration generator, correct? We might want to wait till we figured out what we to do with that (hopefully soon)? |
This is not worth introducing another name than On Dec 29, 2014, at 10:37, Godfrey Chan [email protected] wrote:
|
Global switch sounds good. But what about AR test suite? Should that switch it be turned off for test suite or should be |
We can turn that global switch off for the suite, except for where we are explicitly testing it. On Dec 29, 2014, at 10:52, Josef Šimánek [email protected] wrote:
|
A global switch that changes the meaning of a line of code like this is a very bad idea... which is why I really don't think we do do it in so many other cases. Global switches are exactly how we end up stranding large applications on older versions. |
We have global switches that set the defaults all over the place. In fact, I think this is key to allowing migrations to progress forward. It allows them to upgrade without submitting themselves to keeping up with every default change at the moment of upgrading. The alternative is just to say “this has changed”, and thus requiring to make all the changes to accommodate that change the moment you upgrade. We’ve just seen how laborious that in the Rails code base itself.
|
@simi still interested in finishing this up? |
Sure. Is the global switch good way how to implement this? Should it be turned on for new applications? |
Awesome. Yes and yes. |
@simi Any update on this? |
Agree,
|
How feasible would it be do determine requirement based on db schema? We are (or should be) encouraging good DB design for maintaining referential integrity instead of solely relying on validations which are not as consistent (e.g. model changes in future, application level bugs, batch jobs, performance-sensitive low-level updates, etc. As such, if db table has This would also significantly reduce migration path for users, as I can't imagine a lot of apps who prefer to not have required: true on validation when their schema requires it and would throw db error if trying to save (even with a soft |
Not keen on tying this together. Lots of schemas out there that aren’t likely to change soon, and this change still makes sense for them.
|
@egilburg I think this can be added to gems like schema_validations. |
Rails 5 adds default presence validation on belongs_to associations: rails/rails#18233 Application object need not be present for access token creation in Resource Owner Password Credentials grant flow. This is an alternative to doorkeeper-gem#780 for backwards compatibility with rails 4
Rails 5 adds default presence validation on belongs_to associations: rails/rails#18233. Application object need not be present for access token creation in Resource Owner Password Credentials grant flow. [fixes #780] [fixes #787]
I realize I'm a little late to the party on this one, but what about the concern that now rails associations either fall into a "required" category, or a "not required" category? It seems like this change is now adding opinions to how I should model my data, whereas previously rails just gave me the tools to create whatever unopinionated data model I wanted. Would it not make more sense to relegate this functionality to a gem or a new method, something along the lines of Otherwise how are we deciding which associations are "required" and which are "not required". Has one sounds like it should be required (and belongs_to is almost always the complement of has_one). Also if possible it would be nice to see a deprecation warning included with the validation failure message |
Party is over months ago. Rails is opinionated software. We always had conventions about how your data should be modeled and what the defaults were. This matches more of the use, more of the time. |
http://rubyonrails.org/doctrine/#convention-over-configuration is a helpful guide on how these decisions are made and will continue to be made. |
This seems to be causing an issue with accepts_nested_attributes_for . If I want to create the child object along with the parent, it seems to throwing an error in the child model that the parent has to exist. This seems to be a pretty standard thing to do, right? Is there any workaround for this? Or am I missing something? |
@sandeepravi I'd suggest adding |
@dhh , yeah right now I've made it optional so it seems to work. But I would rather keep it required and not use accepts_nested_attributes. Yeah, nested_attributes in multiple places can get messed up quite soon. You generally prefer to have form objects or just plain POROs? |
I like aggregation through regular Ruby objects. For example, we have a Signup model that's just a Ruby object orchestrating the build process. So I'd give that a go! |
yeah, it seems the simplest option as well. Cool, Thanks! |
@dhh Is there a recommended method for cases where you still need to validate the presence of the child objects on the parent object? I am using |
@ClaudioCarmeli - a year of silence :( Exactly why rails is losing popularity. Brilliant idea - lets change default behavior that has been core for 12 years. Not the way to win the hearts and minds and expand adoption. Also not much in the way of solutions for fixing the issues with nested attributes using |
Almost every belongs_to declaration seems to be a required association. It's rare that you allow your foreign key to be nil. So let's have required: true be the default and required: false be the optional turn-off.
The text was updated successfully, but these errors were encountered: