Monday, July 21, 2014

Using activerecord to find associated models

Let say I have two Model like below

class Plan < ActiveRecord::Base
    has_many :clients

end

class Client < ActiveRecord::Base
    belongs_to :plan

end

 For example I wanted to find all clients which has a plan_id = 5, here is the code
clients = Client.all( :include => :plan, :conditions => [ 'plan_id=?', 5 ] )

No comments :