I have a user who owns many phones
I have a a phone which has many call summaries
therefore my user has many call summaries
Now to the code that I have:
class User < ActiveRecord::Base
has_many :phones
has_many :call_summaries, :through => :phones
end
class Phone < ActiveRecord::Base
belongs_to :user
has_many :call_summaries
end
class CallSummary < ActiveRecord::Base
belongs_to :phones
end
I would like to generate a report that shows all the call summaries for the phones that belong to that certain user. I go into the controller and this is my code there:
def index
@phones = Phone.find(:all, :conditions => ["user_id = ?", @current_user.id])
@call_summaries = @phones.call_summaries.find(:all)
end
But this is returning this error:
undefined method `call_summaries' for #Array:0x476d2d0
Any help would be very much appreciated.
If you have the has_many :through relationship set up, you should just be able to do:
@call_summaries = @current_user.call_summaries
The problem with your method is that you're calling call_summaries
on the @phones collection, rather than on individual phone instances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With