François Beausoleil
2007-Apr-26 00:28 UTC
has_many scope interferes with validates_uniquess_of
Hi all, I have code like this: class Contact < AR::B belongs_to :account_id has_many :email_addresses end class EmailAddress < AR::B belongs_to :account belongs_to :party validates_uniquess_of :address, :scope => :account_id end We use subdomain per account, and that's what account_id represents. Now, if I create an EmailAddress in my tests, my validation rules are properly triggered. In my controller though, I can expose a failure. In the controller, here's how I do the create: class ContactsController < AC def create @party = Party.create!(params[:party]) params[:email_address].each do |id, attrs| @party.email_addresses.create!(attrs) end end end Creating a new contact with an existing E-Mail address works. The generated SQL to ensure validation is this: SELECT * FROM email_addresses WHERE (contact_id = 41) AND (account_id = 17) AND (address = 'bob@test.com') LIMIT 1 Notice the contact scope ? This is obviously not right: the contact scope should not be taken into account when doing the validation. Can anyone confirm / deny my problem ? I'm going to dive into AR's code tomorrow to expose the bug and attempt to correct it. Thanks ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---