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
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 =
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:
scope_note: JUsst involves me
objectives_note: To test the system
- 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.
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
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 =
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’.
Trying to read it as a class
- The class of @mytemplate (again using @mytemplate.class) is given as
= “Array” - ie not the name of the base class, despite what ‘debug’
- Using it as a class (trying @mytemplate.mytype) produces =“undefined
mytype' for #". Oddly, @mytemplate.id produces = 30627456 (the answer should be 1); other possible items don't work, eg @mytemplate.number4 produces: "undefined methodnumber4’ for #”
Trying to read it as an array or hash
- Using it as a hash (eg @mytemplate[‘id’]) produces = “cannot convert
String into Integer”
- Trying it with an integer as the index, ie as an array, (eg
@mytemplate) produces “#”
- Trying it without quotes (eg @mytemplate[id]) produces “” (ie
- Though if I try it with a different key (eg @mytemplate[exercise_id])
I get “undefined local variable or method `exercise_id’ for
- Using it as an array - @mytemplate gives = #
- 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”’))
- When, going back to the view, I inspect @mytype using
@exercise_id.class I get = “Array”
- If I debug mytype (debug @mytype) I get :
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 I get ="#"
6. If I try to dump out the array contents using @mytype 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?