HThomp@aol.com
2006-Jul-03 18:48 UTC
[Rails] Display find(:include => [:children]) results in a view
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 ===================================-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060703/d5ca9173/attachment.html
Chris Carter
2006-Jul-03 19:02 UTC
[Rails] Re: Display find(:include => [:children]) results in a view
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 > ===================================-- Posted via http://www.ruby-forum.com/.
Mark Van Holstyn
2006-Jul-03 19:04 UTC
[Rails] Display find(:include => [:children]) results in a view
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 On 7/3/06, HThomp@aol.com <HThomp@aol.com> 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 > ===================================> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060703/3932e2ad/attachment.html
HThomp@aol.com
2006-Jul-03 23:05 UTC
[Rails] Re: Display find(:include => [:children]) results in a view
The second for loop did the trick, thanks for the help! ------------------------------------------------------------------------------ ------------------------------------------ Date: Mon, 3 Jul 2006 21:02:29 +0200 From: Chris Carter <cdcarter@gmail.com> Subject: [Rails] Re: Display find(:include => [:children]) results in a view To: rails@lists.rubyonrails.org Message-ID: <5dbf89f4afcd3ad59f488a2346adf47d@ruby-forum.com> Content-Type: text/plain; charset=utf-8 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 > ===================================-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060703/193375c7/attachment.html
mixplate
2006-Dec-05 15:59 UTC
Re: Display find(:include => [:children]) results in a view
when do you use include? i thought that with database relationship, ruby automagically gathers all children database information? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- When adding a record in console, a parameter comes in as null even when I set it
- Using set_primary_key breaks acts_as_tree with non-integer column
- How do you do a custom sql call in rails?
- dynamic condition for has_one and eager loading issue
- update_attribute does not update