ActiveRecord::Schema.define() do
create_table :containers do |table|
table.column :name, :string
end
create_table :things do |table|
table.column :description, :string
end
end
class Container<ActiveRecord::Base
has_many :things
end
class Thing<ActiveRecord::Base
belongs_to :container
end
What error do you get? That would help in diagnosing the problem.
You might also want to ask the Ruby on Rails list. I know ActiveRecord
doesn’t have to be used with Rails, but they probably have more people
there with ActiveRecord experience.
ActiveRecord::Schema.define() do
create_table :containers do |table|
table.column :name, :string
end
create_table :things do |table|
table.column :description, :string
end
end
… #but howcome
pocket.things.find(:all) #throws some huge error instead of finding the things in the pocket?
Kyle,
when you have a relationship ‘belongs_to’, you need to define a field
in the database table to references the parent (the name is
<parent_singular>_id), as ActiveRecord does not create this field
automatically. So, we just need a ‘container_id’ in the ‘things’ table:
ActiveRecord::Schema.define() do
create_table :containers do |table|
table.column :name, :string
end
create_table :things do |table|
table.column :description, :string
table.column :container_id, :integer # <-- here
end
end
Your example runs now fine.
You could also add an index (with ‘add_index’) for performance.
Regards
Raul
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.