Forum: Ruby on Rails help... why can't Iuse data from two tables in the same view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9dc56d24553c21738c4bfeff4d743975?d=identicon&s=25 David Upton (davidu)
on 2006-01-21 16:11
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?
This topic is locked and can not be replied to.