JB
2006-Dec-06 15:57 UTC
validates_uniqueness_of where scope euqals created_by "magic" field
I have the following ActiveRecord objects: class Recipe < ActiveRecord::Base has_many :ratings, :dependent => true . . . end class Rating < ActiveRecord::Base validates_uniqueness_of :created_by, :scope => :recipe_id belongs_to :recipe, :counter_cache => true . . . end The created_by field on Rating is implemented as a "magic" field similar to this: http://wiki.rubyonrails.org/rails/pages/Howto+Add+created_by+and+updated_by. It is working fine. The validates_uniqueness_of :created_by, :scope => :recipe_id isn''t working. I can easily go about adding multiple Ratings per Recipe for a user. Maybe I''m missing something here, but what I want is to make sure that a user can only add one Rating per Recipe. I''m guessing that the created_by field is null when the validation runs, and then is populated by the current user when the record is saved. Can anyone shed some light on this and perhaps point me to a better solution? Thanks, JB --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Norris
2006-Dec-06 17:59 UTC
Re: validates_uniqueness_of where scope euqals created_by "magic" field
Probably hack-ish, but you could manually set the created_by field before you save. JB wrote:> I have the following ActiveRecord objects: > > class Recipe < ActiveRecord::Base > has_many :ratings, :dependent => true > . . . > end > > class Rating < ActiveRecord::Base > validates_uniqueness_of :created_by, :scope => :recipe_id > belongs_to :recipe, :counter_cache => true > . . . > end > > The created_by field on Rating is implemented as a "magic" field > similar to this: > http://wiki.rubyonrails.org/rails/pages/Howto+Add+created_by+and+updated_by. > It is working fine. > > The validates_uniqueness_of :created_by, :scope => :recipe_id isn''t > working. I can easily go about adding multiple Ratings per Recipe for a > user. Maybe I''m missing something here, but what I want is to make sure > that a user can only add one Rating per Recipe. I''m guessing that the > created_by field is null when the validation runs, and then is > populated by the current user when the record is saved. Can anyone shed > some light on this and perhaps point me to a better solution? > > Thanks, > > JB > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JB
2006-Dec-06 18:24 UTC
Re: validates_uniqueness_of where scope euqals created_by "magic" field
Thanks. I tried that, but for some reason the validate doesn''t seem to be running. I''m still getting duplicates. If I add an index to the fields at the database level, then Rails throws a duplicate record exception. I wonder if it has something to do with one of my belongs_to declarations. My entire Recipe model code looks like this: class Rating < ActiveRecord::Base validates_presence_of :recipe_id, :value validates_numericality_of :value, :only_integer => true validates_uniqueness_of :created_by, :scope => :recipe_id belongs_to :recipe, :counter_cache => true belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" end Jason Norris wrote:> Probably hack-ish, but you could manually set the created_by field > before you save. > > JB wrote: > > I have the following ActiveRecord objects: > > > > class Recipe < ActiveRecord::Base > > has_many :ratings, :dependent => true > > . . . > > end > > > > class Rating < ActiveRecord::Base > > validates_uniqueness_of :created_by, :scope => :recipe_id > > belongs_to :recipe, :counter_cache => true > > . . . > > end > > > > The created_by field on Rating is implemented as a "magic" field > > similar to this: > > http://wiki.rubyonrails.org/rails/pages/Howto+Add+created_by+and+updated_by. > > It is working fine. > > > > The validates_uniqueness_of :created_by, :scope => :recipe_id isn''t > > working. I can easily go about adding multiple Ratings per Recipe for a > > user. Maybe I''m missing something here, but what I want is to make sure > > that a user can only add one Rating per Recipe. I''m guessing that the > > created_by field is null when the validation runs, and then is > > populated by the current user when the record is saved. Can anyone shed > > some light on this and perhaps point me to a better solution? > > > > Thanks, > > > > JB > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---