Area with condition on different models

I have the 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 want to create a region in a model element to retrieve only records with active transactions, for example:

 User.find(1).items.active 
+4
source share
1 answer

I have found the answer. it will be like this:

 scope :active, joins(:transactions) Transaction.active 

The answer was here: http://asciicasts.com/episodes/215-advanced-queries-in-rails-3

+3
source

Source: https://habr.com/ru/post/1383184/


All Articles