views:

61

answers:

2

Hello I have no good title for this question, but my problem is to set a conditon for my sub-table in rails I have a model named "users" and another named "hours", hours is set to "belongs_to :users" and the users-model has "has_many :hours"

But my problem is when I try to fetch the users, but just the hours added this month. I want this to happen only in the Index action.

How can I set a condition for my sub table?

+1  A: 
class SomeController
  def index
    Hours.all(:include => [:user], :conditions => "added_this_month is null")
  end
end
rnicholson
Almost what I wanted, but is it posible to return the users who haven't any hours also? (JOIN LEFT?)
Terw
Solution: http://stackoverflow.com/questions/1509692/rails-activerecord-joins-with-left-join-instead-of-inner-join
Terw
Oh well... just edited anyway. Since I noticed you were interested more in hours vs. users.
rnicholson
A: 

has_many can use :conditions

has_many :hours, :conditions => ...

Jim