Q:

Rails using has_many through associations

this is a noob question- i have 3 associated tables i am trying to access.
The Patient model has:
has_many :charts
has_many :providers, :through => :charts

The Provider model has:
has_many :charts 
has_many :patients, :through => :charts

and the Chart model has:
belongs_to :patient
belongs_to :provider

i pull the current_user.id to setup the @provider which works fine.
@provider = Provider.where(:user_id => current_user.id).first    

then i use that @provider.id to create a hash of charts i need which also works
@charts = Chart.where(:provider_id => @provider.id)

but then when i try to create a hash of the patients from the @charts hash it breaks....
@patients = Patient.where(:id => @charts.patient_id)

says 'undefined method `patient_id' for #'
[patient_id is one of the columns in the charts table]
What am i doing wrong? Help! 

A:

Try this:
@patients = Patient.where(:id => @charts.map(&:patient_id))

The @charts collection doesn't automatically collect the patient_id's of it's members.  The other two queries work because you're acting on a single record, whereas in the last query, you're acting on the returned collection from the second query.

