148

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
You Nguyen
  • 9,961
  • 4
  • 26
  • 52
Tornskaden
  • 2,203
  • 2
  • 15
  • 12
  • 1
    They do work the same -- is that such a bad thing? references inserts a foreign key column for you. You could use belongs_to instead to make your migration more readable. See http://guides.rubyonrails.org/migrations.html for some details. – muffinista Oct 17 '11 at 00:24
  • 1
    Not saying it is a bad thing. Just got confused about if they do work the same or in different ways, since I can find no documentation specificaly saying they work the same. [http://guides.rubyonrails.org/migrations.html](http://guides.rubyonrails.org/migrations.html) is also not clear on that matter. – Tornskaden Oct 17 '11 at 01:04
  • 3
    This is likely just for backward compatibility and `references` will be deprecated and removed in a future release of rails. Don't take my word on this, it's just an educated guess. – bricker Oct 17 '11 at 07:06
  • 1
    "The other helper is called references (also available as belongs_to). In its simplest form it just adds some readability" -- from the guide – muffinista Oct 17 '11 at 13:42
  • 6
    Also here's the source code -- belongs_to is a straight alias of references https://github.com/rails/rails/blob/88aa2efd692619e87eee88dfc48d608bea9bcdb4/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L421 – muffinista Oct 17 '11 at 13:53
  • @muffinista: Your source code link is perfekt. Please place an answer so I can mark it as an accepted answer. – Tornskaden Oct 17 '11 at 14:17

1 Answers1

194

Looking at the source code, they do the same exact thing -- belongs_to is an alias of reference:

  def references(*args)
    options = args.extract_options!
    polymorphic = options.delete(:polymorphic)
    args.each do |col|
      column("#{col}_id", :integer, options)
      column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
    end
  end
  alias :belongs_to :references

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 to references for other sorts of associations.

muffinista
  • 6,676
  • 2
  • 30
  • 23
  • I wonder if they are going to keep it like this in the future or they are going to remove one of them!? I like it this way, being able to choose whichever makes my code look more like real english. – Tornskaden Oct 17 '11 at 20:22
  • I would guess that both are here to stay. Looking at the commit logs, it's been this way since 2007. – muffinista Oct 17 '11 at 20:55
  • 4
    IMO, `references` is an ambiguous choice for a term. They renamed `before_filter` to `before_action`, which was a good move because it reduced ambiguity. One advantage of `references` is that it's simply different from what you use in the model, so you get less confused if you're in the model or migration. But any term that differs would satisfy this criteria. – ahnbizcad Sep 12 '14 at 12:27
  • 4
    IMO `references` is better term in db-level. – vasilakisfil Dec 04 '14 at 13:41
  • 1
    I don't really think they actually mean the same thing in English... So it's a bit weird. – xji Jan 08 '15 at 09:55