Rails relations

problem

You know when you want to select  records depending on the values of related records (like belongs_to fields) ?

scenario

class TradingSignal < ActiveRecord::Base
  belongs_to :product
end
class Product < ActiveRecord::Base
   has_many :trading_signals, :dependent => :destroy
end

 

Great way of getting all TradingSignals where its related product are one of the given ID’s

  TradingSignal.joins(:product).where(:products => {:id => ids})

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.