Determine if count of related model > 0

Posted by Lowgain on Stack Overflow See other posts from Stack Overflow or by Lowgain
Published on 2010-05-24T01:16:20Z Indexed on 2010/05/24 1:20 UTC
Read the original article Hit count: 293

Filed under:
|

I have a model called Stem. I need a 'thumbs up' feature, so I have created a second model called Thumb, which consists of stem_id and user_id.

I'm also using the restful authentication plugin for user credentials.

I have the 'thumbs up' button working, which adds a row to the thumbs table fine, but I'd like to be able to check if the currently logged in user has already given a thumbs up to this particular stem.

I tried adding this to the Stem model:

def thumbed
    Thumb.count_by_sql ["SELECT COUNT(*) FROM thumbs WHERE user_id = ? AND stem_id = ?", current_user.id, self.id ]
end

The problem here is that the stem model has no access to the current_user variable the the controllers have.

Is there a way I can get access to this property, or alternatively, is there another way I could go about checking this? I was hoping to get this as a property in the model because the stems are passed over to a Flex app using RubyAMF.

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about model