Reputation: 834
Suppose you have a model User
and a model Challenge
.
One User
can be the supervisor of a challenge. Therefor a Challenge
has a belongs_to
relation with the User.
The relation is declared as following
in challenge.rb
:
class Challenge < ActiveRecord::Base
belongs_to :supervisor, :class_name => "User", :foreign_key => "user_id"
Resulting in the following schema.rb
:
create_table "challenges", :force => true do |t|
t.string "title"
t.text "description"
t.datetime "start_date"
t.datetime "end_date"
t.string "state"
t.integer "count"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
# Notable line:
t.integer "user_id"
end
Should the foreign key in the schema be named supervisor_id
or user_id
?
Upvotes: 0
Views: 98
Reputation: 35533
By convention, you would have a simpler time if you named it supervisor_id
for 2 reasons:
:foreign_key
modifier in the belongs_to
declarationbelongs_to
relationship that links to a generic user, you can call that one user_id
without needing to rename anythingOther than that, it really doesn't matter. That is, Rails won't care. And it certainly isn't worth the time for a 'heated discussion'.
Upvotes: 3