开发者

How to make a foreign key connect with sqlite3 and Ruby on Rails

开发者 https://www.devze.com 2023-01-01 05:32 出处:网络
Does anyone know the way, or a place where I can find开发者_JS百科 out how to do this? Basically, all I want to do is connect a foreign key between two tables.

Does anyone know the way, or a place where I can find开发者_JS百科 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

0

精彩评论

暂无评论...
验证码 换一张
取 消