Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails undefined method for array

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.

like image 264
Ryan Avatar asked Jul 31 '09 17:07

Ryan


1 Answers

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.

like image 185
Greg Campbell Avatar answered Oct 10 '22 00:10

Greg Campbell