(Rails 2.3.4) Can''t quite figure out what I''m doing wrong but I''m seeing different behavior for validates_uniqueness_of between two models that look more ore less identical to me -- I have a model called Person: class Person < ActiveRecord::Base validates_uniqueness_of :first_name, :scope => :last_name end I also have a model called Setting (application settings object): class Setting < ActiveRecord::Base validates_uniqueness_of :setting, :scope => :category {some class methods} end Here''s the schemas: create_table "people", :force => true do |t| t.string "last_name" t.string "first_name" t.datetime "created_at" t.datetime "updated_at" end create_table "settings", :force => true do |t| t.string "setting" t.string "value" t.float "num_value" t.string "category" t.string "tag" t.datetime "created_at" t.datetime "updated_at" end When I try to add a Person where two rows have the same first name, but different last names, I''m able to save both records successfully. However when I try to add a Setting where two rows have the same ''setting'', but different ''category'', I got an ActiveRecord::RecordInvalid: Validation failed: Setting has already been taken Interstingly, the executed SQL in the log file when checking enforcement of the constraint seems to be different for the two models, although so far as I can see, the structure is similar in all the relevant dimensions: SELECT `people`.id FROM `people` WHERE (`people`.`first_name` = BINARY ''jack'' AND `people`.last_name = ''kramer'') LIMIT 1 whereas the check performed for dupes in setting is: SELECT * FROM `settings` WHERE (`settings`.`setting` IN (''`settings`.`setting` = BINARY ? AND `settings`.category ?'',''width'',''frame'')) LIMIT 1 (Note that his returned "false" even though there is no Setting where s.category = "frame" What am I doing wrong? Why is genereated sql for People simply "where a and b" with for Settings it is where (a in (b and c))? Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gabriel Saravia
2011-Mar-26 01:30 UTC
Re: inconsistent validate_uniqueness_of & :scope behavior?
It may not be the problem, but I think there is some strangeness there in that you have a model called Setting and an attribute called setting. possibly, Rails is getting confused - is it validating uniqueness of setting the attribute, or is it thinking that there can be only one Setting instance period...? Try changing either your model name or that ''setting'' attribute name. and see if that fixes it... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.