I am having trouble when using a child to reference the parent object
in a relationship, it always claims the parent is nil. I could easily
be doing something wrong as this is all hazy but I thought I’d ask if
anyone could see anything obvious:
I have two databases:
rulesets
id
name
races
id
name
ruleset_id
A race specifies it’s ruleset so, a ruleset can have many races, but
each race will have only 1 ruleset. I have specified a foreign key
relationship without a constraint (using mysql “foreign key
(ruleset_id) references rulesets (id)”) but my version of MySQL
doesn’t even enforce foreign keys so that shouldn’t help much. In the
models I have:
class Ruleset < ActiveRecord::Base
has_many :races
…
class Race < ActiveRecord::Base
belongs_to :rulesets
…
Then in the controller I have:
@races = Race.find( :all )
And finally in the view:
<% @races.each do |r| %>
…
<%= @r.ruleset.name %>
…
And it complains that ruleset is not a valid method for races. The
book I have on Ruby only talks about doing this sort of access in one
direction (i.e. ruleset.races. not
races.ruleset.) so I’m not even sure if it’s possible to
have a child accessing the values of it’s parent. But if it is, what
am I doing wrong here?
Thanks in advance!!
Chris