Mike Neumegen
Mike Neumegen

Reputation: 2486

Get number of rows associated model has with complicated join

I'm new to rails and am unsure of the best way of doing this. I have a User which "has many" Events. I want to be able to go user.active? in a view which will display whether the user has one or more associated Events created in the last 5 minutes.

I know the syntax isn't correct but this is what I've got so far:

def active?
    find(:conditions => {:events => ["created_at >= ?", DateTime.now - 5.minute]}).count > 0
end

Upvotes: 1

Views: 649

Answers (1)

Mischa
Mischa

Reputation: 43298

You can just do a find on the events collection:

def active?
  events.where('created_at >= ?', 5.minutes.ago).count > 0
end

Upvotes: 1

Related Questions