Help for newbie please!
I have two tables: controls and programs.
controls has_many :programs Programs belongs_to :control
Controls has id, controlname Programs has, apart from it's own id, two fields ctl1_id and ctl2_id.
These represent two controls that the program relates to.
Foreign keys are set on program table columns ctl1_id and ctl2_id back to the id field on the control table.
Let's make this more explicit:
class Control < ActiveRecord::Base has_many :programs end
class Program < ActiveRecord::Base belongs_to :ctl1, :class_name => 'Control', :foreign_key => 'ctl1_id' belongs_to :ctl2, :class_name => 'Control', :foreign_key => 'ctl2_id' end
So when I list the program table using
<% @programs.each do |array| %> <tr> <td><%= array.ctl1_id%></td> <td><%= array.ctl2_id %></td> </tr> <% end %>
...everything is fine.
Any of these: <% @programs.each do |program| %> <% @programs.each do |p| %> <% for program in @programs %>
is probably better than "array" for the block variable. (But this is a personal choice as long as you avoid keywords like "class" or "begin".)
But when I list the programs row I want to actually show the controlname from controls table rather than the ctl1_id and ctl2_id values from the programs row. For this I use the following code:-
<% @programs.each do |array| %> <tr> <td><%= array.control.controlname %></td> </tr> <% end %>
I expect the link via the foreign keys and the models relationships to give me what I need but I am obviously missing something because I get this:-.
You have a nil object when you didn't expect it! The error occured while evaluating nil.control
Extracted source (around line #56):
53: </tr> 54: <tr> 55: 56: <%= array.control.controlname %></td>
I have checked out the cookbook application code and cant see where I am misunderstanding.
Any help gratefully received.
I can see that
<% @programs.each do |program| %> <tr> <td><%= program.ctl1.controlname %></td> <td><%= program.ctl2.controlname %></td> </tr> <% end %>
But personally, I'd rename "controlname" to just "name" since it's a column in the Control model anyway.
From this little example, it seems like you have a program with many (ok, 2) controls, but does each control really associate to more than one program? I'm just wondering whether there's another join model linking programs and controls in a many-to-many relationship.
Anyway, I suspect that you are under the mistaken assumption that the existence of foreign keys in the database have some influence on the behavior of your ActiveRecord models. Other than potentially causing inserts or deletes to fail if performed in the wrong order, your Models won't care.
Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com