I am trying to determine how to display a list returned from a Rails
find(:include => [:children]). I am using the following (which works in
the
console):
@patients = Account.find(session[:account_id]).patients.find(:all,
:include
=> [:patient_details])
How can I address the patient_details to display them in the view? The
console even shows a @patient_details instance variable being
created…
Any ideas?
Models:
class Account < ActiveRecord::Base
has_many :account_patients
has_many :patients,
:through => :account_patients
end
class AccountPatient < ActiveRecord::Base
belongs_to :account
belongs_to :patient
belongs_to :patient_detail
end
class Patient < ActiveRecord::Base
has_many :account_patients
has_many :accounts,
:through => :account_patients
has_many :patient_details
end
class PatientDetail < ActiveRecord::Base
belongs_to :patient
end
Hello, You need to do something in your view that gets those details.
You probably want to use something like:
<% for patient in @patients %>
<% for detail in patient.patient_detail %>
<%= detail.description %>
<% end %>
<% end %>
Good luck with your project!
unknown wrote:
I am trying to determine how to display a list returned from a Rails
find(:include => [:children]). I am using the following (which works in
the
console):
@patients = Account.find(session[:account_id]).patients.find(:all,
:include
=> [:patient_details])
How can I address the patient_details to display them in the view? The
console even shows a @patient_details instance variable being
created…
Any ideas?
Models:
class Account < ActiveRecord::Base
has_many :account_patients
has_many :patients,
:through => :account_patients
end
class AccountPatient < ActiveRecord::Base
belongs_to :account
belongs_to :patient
belongs_to :patient_detail
end
class Patient < ActiveRecord::Base
has_many :account_patients
has_many :accounts,
:through => :account_patients
has_many :patient_details
end
class PatientDetail < ActiveRecord::Base
belongs_to :patient
end
For starters lets clean up the find to do all the work at once…
@account = Account.find( session[:account_id], :include => {:patients =>
:patient_details } )
This will allow you to pull in the account, all the patients, and all
the
patient details at once, with out doiny many sql queries. In your view
you
will have an @account variable. If you still want you @patients variable
just put this in your controller as well…
@patients = @account.patients
You should have be able to access the patient details from your patient
model…
@patients.each do |patient|
patient.patient_details.each do |detail|
#do something with the PatientDetail detail here…
end
end
when do you use include? i thought that with database relationship, ruby
automagically gathers all children database information?