Skip to content

Add a required option to singular associations#16056

Merged
dhh merged 1 commit intorails:masterfrom
sgrif:sg-required-associations
Jul 5, 2014
Merged

Add a required option to singular associations#16056
dhh merged 1 commit intorails:masterfrom
sgrif:sg-required-associations

Conversation

@sgrif
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

sgrif commented Jul 4, 2014

/cc @dhh

@dhh
Copy link
Copy Markdown
Member

dhh commented Jul 4, 2014

Looks great to me 👍!

@dhh
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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