There are two lines that you need in the comment model. Rather than
a comment has_one user, it belongs_to a user. The difference is that
foreign key is located in the comment table.
By declaring the symbol :user in these method calls, Active Record
automatically finds the class User and the field in the comments table
user_id and maps these rules that that field.
The belongs_to method works hand in hand with the has_many and has_one
methods in the corresponding models. Basically, if the model has a
key field in it’s table, then in needs a belong to. Your not
only use the user_id field name for user. You can tell AR to map a
key to any field.
ie for projects you may have fields in the table (to map to a user
in the project model
belongs_to :creator, :foreign_key => “created_by”, :class_name => “User”
belongs_to :project_manager, :foreign_key => “project_manager_id”,
:class_name => “User”
in the user model
has_many :project_managements, :class_name => “Project”, :foreign_key =>
has_many :created_projects, :class_name => “Project”, :foreign_key =>
There are a number of options for both of these method calls that can be
found in the docs.
Hopefully this helps.