Yeah, this is sort of what I did in a rating model.

  belongs_to :ratable, :polymorphic => :true
  belongs_to :rating_category

  validates_uniqueness_of :ratable_id,
    :scope => [:rating_category_id, :ratable_type, :user_id],
    :message => "is already set."

  include OwnedModel

Owned model includes a belongs_to :user, :creator => true and some
default ownership permissions.  Hmm.. in fact, down think it checks
permissions-wise.

On Aug 2, 11:55 am, kevinpfromnm <[email protected]> wrote:
> 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.

Reply via email to