David Upton
2006-Jan-21 15:11 UTC
[Rails] help... why can''t Iuse data from two tables in the same view
I am new to Rails and Ruby, and to OO languages, and seem to be making a very silly mistake somewhere here. Can anybody help? I am trying to write an application which involves ''exercises'', each of which consists of several ''templates''. This is based on MySQL tables with these names. I have models and controllers, built with the Rails Scaffold, for both these tables. The ''exercises'' controller is called Setup-controller and the templates controller is called Design_controller. My problem is that I want to show, on the same web page, data for a given Exercise, and for the Templates linked to it. In the setup controller, I have written a simple ''hello'' method. This tries to draw data from both the exercise and templates tables. It selects the exercise based on the passed parameter: @newex = Exercise.find(params[:id]) I can easily pass exercise class items to my view. In the view, the class of @newex (found by @newex.class) is given as "Exercise" - ie the name of the base class If in the view I debug @newex, it gives me: --- !ruby/object:Exercise attributes: logistics_id: scope_note: JUsst involves me scenario_note: '''' players_id: logistics_note: '''' objectives_note: To test the system (etc...) - which is what I expect; and I can easily dump out individual values in the view, eg @newex[''objectives_note''] gives me = "To test the system" which is correct. Template Items. Then in the same ''hello'' method in the setup controller I define @mytemplate: @mytemplate = Template.find_by_sql(''select * from templates where exercise_id = "1"''). (in other words, trying to access the other database table.) I have included "model :template" in the setup controller and also in the application controller. Also, the template model (template.rb) includes "belongs_to :exercises" and the exercises model includes " has_many :templates ". The problem comes when I try to make the Template model available in the view. First, in the view, I debug @mytemplate --- - !ruby/object:Template attributes: text8: number4: "1" exercise_id: "1" mytype: f id: "1" (etc...) This is the correct information being passed to the view - note that the debug headers are identical except for the base class name! - but from here on I can only access this information in the view by using ''debug''. For instance: Trying to read it as a class 1. The class of @mytemplate (again using @mytemplate.class) is given as = "Array" - ie not the name of the base class, despite what ''debug'' shows. 2. Using it as a class (trying @mytemplate.mytype) produces ="undefined method `mytype'' for #". Oddly, @mytemplate.id produces = 30627456 (the answer should be 1); other possible items don''t work, eg @mytemplate.number4 produces: "undefined method `number4'' for #" Trying to read it as an array or hash 1. Using it as a hash (eg @mytemplate[''id'']) produces = "cannot convert String into Integer" 2. Trying it with an integer as the index, ie as an array, (eg @mytemplate[0]) produces "#" 3. Trying it without quotes (eg @mytemplate[id]) produces "" (ie nothing). 4. Though if I try it with a different key (eg @mytemplate[exercise_id]) I get "undefined local variable or method `exercise_id'' for #<#:0x39d5db0>" 5. Using it as an array - @mytemplate[0] gives = # 6. If I ask for the array length, using array.length, I get = "1", which can''t be right. Defining and using individual string variables So I went back to the setup controller and tried to define individual rows from the table as variables (eg @mytype = Template.find_by_sql(''select mytype from templates where id = "1"'')) 1. When, going back to the view, I inspect @mytype using @exercise_id.class I get = "Array" 2. If I debug mytype (debug @mytype) I get : --- - !ruby/object:Template attributes: mytype: f which is correct: the value should be ''f''. Note that here the ''Template'' base class is correctly given. 3. If I ask for the array length, using array.length, I get = "1" 4. If I try to include @mytype in my view as a string, I get: "#". 5. If I try to dump out the array contents using @mytype[0] I get ="#" 6. If I try to dump out the array contents using @mytype[1] I get ="" 7. If I try to dump out the array contents using @mytype[''1''] I get ="cannot convert String into Integer" Sorry to ask what is probably a very silly question, but I just can''t find the answer. How can I allow one view to access data from two (linked) tables, which in the Rails Scaffold use different models and controllers. What have I missed? -- Posted via http://www.ruby-forum.com/.
Nick Horrigan
2006-Feb-06 21:28 UTC
[Rails] help... why can''t Iuse data from two tables in the same view
David I''ve having exactly the same problem here and can''t find a solution - have you had any joy? Nick Horrigan ROL E: nick.horrigan@rol.co.uk T: 01572 725454 M: 07917 464756 W: www.rol.co.uk This email and any files transmitted with it are confidential, may be legally privileged and are intended solely for the use of the addressee. If you have received this email in error you are requested to contact the sender immediately, and not to disclose or make use of this information. Although rol operates an active anti-virus policy, the organisation accepts no liability for any damage caused by any virus transmitted by this email, including any attachments. The views contained in this email are those of the author and not necessarily those of rol. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060206/d43e1b76/attachment.html
Craig White
2006-Feb-06 21:45 UTC
[Rails] help... why can''t Iuse data from two tables in the same view
On Mon, 2006-02-06 at 21:29 +0000, Nick Horrigan wrote:> David > > I''ve having exactly the same problem here and can''t find a solution - > have you had any joy? >---- I am working on this at the moment... What has worked for me... in myfile.rhtml <%= this_controller.field_name %> <%= this_controller.another_controller.field_name %> and in this_controller.rb def myfile @this_controller This_controller.find(params[:id]) @another_controller AnotherController.find(:all) end and also, you must set your has/belongs to/ etc. in the models I hope this helps Craig
David Mitchell
2006-Feb-06 22:06 UTC
[Rails] help... why can''t Iuse data from two tables in the same view
Assume you want to render a view linking people and jobs together, for some hypothetical application. You''ve got two tables, ''people'' and ''jobs'', that you want to drag info from for this one form. In your controller: jobs_controller.rb ... def link_people_to_jobs @jobs = Job.find(:all) @people = Person.find(:all) ... end Then, in your view: <% @jobs.each do |job| @people.each do |person| ...(do cool stuff here) end end %> Note that you should *define* @jobs and @people in your controller, not try to define them in your view. Regards Dave M. On 07/02/06, Nick Horrigan <Nick.Horrigan@rol.co.uk> wrote:> > > David > > I''ve having exactly the same problem here and can''t find a solution - have > you had any joy? > > Nick Horrigan > ROL > E: nick.horrigan@rol.co.uk > T: 01572 725454 > M: 07917 464756 > W: www.rol.co.uk > > This email and any files transmitted with it are confidential, may be > legally privileged and are intended solely for the use of the addressee. If > you have received this email in error you are requested to contact the > sender immediately, and not to disclose or make use of this information. > Although rol operates an active anti-virus policy, the organisation accepts > no liability for any damage caused by any virus transmitted by this email, > including any attachments. The views contained in this email are those of > the author and not necessarily those of rol. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >