views:

63

answers:

1

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" ?

+1  A: 

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

Greg
This is "exactly" what I was looking for. Thank you so much.
Trip