Does anyone know the way, or a place where I can find out how to do this?
Basically, all I want to do is connect a foreign key between two tables.
Is it true, that all I have to do is write the "belongs_to" and "has many" ?
Does anyone know the way, or a place where I can find out how to do this?
Basically, all I want to do is connect a foreign key between two tables.
Is it true, that all I have to do is write the "belongs_to" and "has many" ?
You also need to ensure that a column exists for the foreign key in the database table associated with the Class that says it "belongs_to" the other one. So for the classes...
Class Tree
belongs_to :forest
end
Class Forest
has_many :trees
end
...Rails assumes that your trees
table has a forest_id
column. You can then do, for example,
my_tree = Tree.find(1)
my_trees_forest = my_tree.forest
Here's a great place to get the info you need: http://guides.rubyonrails.org/association_basics.html