Reputation: 115
i have following code:
class Item < ActiveRecord::Base
belongs_to :user
has_many :transactions
#scope :active, lambda??
end
class Transaction < ActiveRecord::Base
belongs_to :user
belongs_to :item
scope :active, where("status = 0")
end
class User < ActiveRecord::Base
has_many :items
has_many :transactions
end
i wish to build a scope in model Item to retrieve only records with active transactions, for example:
User.find(1).items.active
Upvotes: 4
Views: 2254
Reputation: 115
I found the answer. it shoud be this way:
scope :active, joins(:transactions)
Transaction.active
The answer was here: http://asciicasts.com/episodes/215-advanced-queries-in-rails-3
Upvotes: 3