Skip to content
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

Add a required option to singular associations #16056

Merged
merged 1 commit into from
Jul 5, 2014

Conversation

sgrif
Copy link
Contributor

@sgrif sgrif commented Jul 4, 2014

In addition to defining the association, a required association will
also have its presence validated.

Before:

belongs_to :account
validates_presence_of :account

After:

belongs_to :account, required: true

This helps to draw a distinction between types of validations, since
validations on associations are generally for data integrity purposes,
and aren't usually set through form inputs.

@sgrif
Copy link
Contributor Author

sgrif commented Jul 4, 2014

/cc @dhh

@dhh
Copy link
Member

dhh commented Jul 4, 2014

Looks great to me 👍!

@dhh
Copy link
Member

dhh commented Jul 4, 2014

Build failed?

In addition to defining the association, a `required` association will
also have its presence validated.

Before:

```ruby
belongs_to :account
validates_presence_of :account
```

After:

```ruby
belongs_to :account, required: true
```

This helps to draw a distinction between types of validations, since
validations on associations are generally for data integrity purposes,
and aren't usually set through form inputs.
@sgrif
Copy link
Contributor Author

sgrif commented Jul 4, 2014

Issue with mysql and the test cases I added, updated, should pass on this run.

dhh added a commit that referenced this pull request Jul 5, 2014
Add a `required` option to singular associations
@dhh dhh merged commit 3ac8118 into rails:master Jul 5, 2014
sgrif added a commit that referenced this pull request Jul 5, 2014
@sgrif sgrif deleted the sg-required-associations branch July 5, 2014 00:51
@guilleiguaran
Copy link
Member

❤️

huoxito added a commit to spree/spree that referenced this pull request Oct 23, 2014
Not sure this ever worked as expected see:

rails/rails@0950d40
rails/rails#16056

for a bit of history
JDutil pushed a commit to JDutil/spree that referenced this pull request Mar 31, 2015
Not sure this ever worked as expected see:

rails/rails@0950d40
rails/rails#16056

for a bit of history
@claudiob
Copy link
Member

@sgrif The documentation added by this PR says:

This will validate the association itself, not the id. You can use
+:inverse_of+ to avoid an extra query during validation.

This sentence is still part of the current documentation. The way in which I interpret this is that:

  • calling belongs_to :user will make an extra query SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] to validate that the actual association is valid
  • but calling belongs_to :user, inverse_of: false will not make that extra query

However that is not the case. The extra query still takes place since this code is still running:

if reflection.options[:required]
  model.validates_presence_of reflection.name
end

Can you help me understand if I'm missing something?
I'd like to use belongs_to and (if possible) not incur in that extra query, validating only the presence of user_id and not its association.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants