What is the difference between t.references
and t.belongs_to
?
Why are we having those two different words? It seems to me they do the same thing?
Tried some Google search, but find no explanation.
class CreateFoos < ActiveRecord::Migration
def change
create_table :foos do |t|
t.references :bar
t.belongs_to :baz
# The two above seems to give similar results
t.belongs_to :fooable, :polymorphic => true
# I have not tried polymorphic with t.references
t.timestamps
end
end
end
Looking at the source code, they do the same exact thing --
belongs_to
is an alias ofreference
:This is just a way of making your code more readable -- it's nice to be able to put
belongs_to
in your migrations when appropriate, and stick toreferences
for other sorts of associations.