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

Relations validate: true only runs on create #24532

Closed
jhaesus opened this issue Apr 13, 2016 · 5 comments
Closed

Relations validate: true only runs on create #24532

jhaesus opened this issue Apr 13, 2016 · 5 comments

Comments

@jhaesus
Copy link

jhaesus commented Apr 13, 2016

Steps to reproduce

class Parent < ApplicationRecord
  has_many :items, validate: true
end

class Item < ApplicationRecord
  belongs_to :parent
  validates :name, presence: true
end

Expected behavior

item1 = Item.new
parent1 = Parent.new
parent1.items << item1
parent1.save! => Validation error

item2 = Item.new(name: "bla")
parent2 = Parent.new
parent2.items << item2
parent2.save! => true
item2.name = nil
item2.save(validate: false)
parent2.reload.save! => true

Actual behavior

items are not validated on update

System configuration

Rails version: 5.0.0.beta3

Ruby version: 2.3.0

tomash added a commit to tomash/rails that referenced this issue Apr 13, 2016
@tomash
Copy link
Contributor

tomash commented Apr 13, 2016

I don't think it's a bug. From what I see in the code, validate option only ensures validation when inserting new record into association:

def insert_record(record, validate = true, raise = false)

def insert_record(record, validate = true, raise = false)

If you want to ensure all associated records are revalidated on every update, you should use validates_associated. Here, I've added test cases demonstrating what you expect (and that they fail if validates_associated is not used:

tomash@b701d92

@tomash
Copy link
Contributor

tomash commented Apr 13, 2016

My personal suggestion: update the docs to reflect that validate: true causes validation only on inserting new member of association, not that it's the same as validated_associated. Current description "If false, don't validate the associated objects when saving the parent object. true by default." can lead to confusion. I can file a docs pull request if maintainers are OK with this idea?

@vipulnsward
Copy link
Member

Even validated_associated doesn't work.

@tomash
Copy link
Contributor

tomash commented Apr 13, 2016

@vipulnsward works for the test cases I've written. Is there a case that fails?

@sgrif
Copy link
Contributor

sgrif commented Apr 13, 2016

I'd be fine with changing the docs to clarify this. Even if this was considered a bug, it would be extremely difficult to change. Since validate: true is the default for collection associations, changing the behavior of that option would be a massive breaking change.

@sgrif sgrif closed this as completed Apr 13, 2016
tomash added a commit to tomash/rails that referenced this issue Apr 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants