I'm thinking perhaps the simplest way to do this, is to add a uniqueness validation and make sure the item is valid in creation.
On Jul 31, 4:54 am, Nicolas Oury <[email protected]> wrote: > Hi again, > > First, thank you very much for your help. > I have a follow-up question. I want to add a LikeIt button, that > people can press to say they like something. > Then, I print a counter on the page. > > I followed your code for comments and just added: > > belongs_to :loveable, :polymorphic => :true, :counter_cache => true > named_scope :lover, lambda {|user| { :conditions => ['owner_id = ?', > user.id] } } > named_scope :loves, lambda {|item| { :conditions => ['loveable_id = ? > AND loveable_type = ?', item.id, item.class.to_s] } } > > and > > def create_permitted? > acting_user.signed_up? && (owner_is? acting_user) && > Likeit.lover(acting_user).loves(loveable).empty? > end > > To make sure every user can only an item once. > > It kinda works, except for this: > If I do not rerender the button after it is clicked, it can be clicked > multiple times. > If I look in the server log, the create_permited? seems to be called > when the page is rendered, not when the > Likeit field is created. > > Is it normal or I am doing something wrong? > How to make sure create_permitted? is called before saving anything to > the database? > > Thanks again. Your help is very much appreciated. > If it can be useful, I would be very happy to write a recipe on > comments and likeit buttons, once I have understood. > > Best regards, > > Nicolas. -- You received this message because you are subscribed to the Google Groups "Hobo Users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
