I am having some difficulty correctly setting up the relationships
between two ActiveRecord model. The DB tables look something like this:
table scripts {
id :integer
description :text
}
table households {
id :integer
script_id :integer
}
There are a small number of scripts and a large number of households.
Each household has exactly one script.
I have a household but when I call household.Script.description I am not
getting the correct result. household.Script is returning nil if I
define the relationship as follows (there is definitely a matching
record in the scripts table for household.script_id):
class Script < ActiveRecord::Base{
has_many :Household
}
class Household < ActiveRecord::Base {
belongs_to :Script
}
I get “Mysql::Error: Unknown column ‘scripts.household_id’ in ‘where
clause’: SELECT * FROM scripts WHERE (scripts.household_id = 7820)
LIMIT 1” if I define it as:
class Script < ActiveRecord::Base{
has_many :Household
}
class Household < ActiveRecord::Base {
has_one :Script
}
I am having some difficulty correctly setting up the relationships
between two ActiveRecord model. The DB tables look something like this:
table scripts {
id :integer
description :text
}
table households {
id :integer
script_id :integer
}
There are a small number of scripts and a large number of households.
Each household has exactly one script.
I have a household but when I call household.Script.description I am not
getting the correct result. household.Script is returning nil if I
define the relationship as follows (there is definitely a matching
record in the scripts table for household.script_id):
class Script < ActiveRecord::Base{
has_many :Household
}
class Household < ActiveRecord::Base {
belongs_to :Script
}
I get “Mysql::Error: Unknown column ‘scripts.household_id’ in ‘where
clause’: SELECT * FROM scripts WHERE (scripts.household_id = 7820)
LIMIT 1” if I define it as:
class Script < ActiveRecord::Base{
has_many :Household
}
class Household < ActiveRecord::Base {
has_one :Script
}
Any ideas?
Your second configuration is definitly wrong, Household should definitly
have only one Script but the syntax should be like your first example.
i don’t really know how you are getting the wrong results but i have
some few ideas.
use undercase for the relations names (has_many :households,
belongs_to :script).
If you cleared your database data, Maybe your database’s sequences
did not restart and old id’s are being used.