user1611830
user1611830

Reputation: 4857

rails belongs_to customization

I read in some code this

attr_accessible :namespace_id, :creator_id, as: :admin
belongs_to :creator,      foreign_key: "creator_id", class_name: "User"

Why not simply declare a user_id in the model (and in table), and then simply use belongs_to :user ? Btw, is now creator an alias to creator_id ?

Upvotes: 0

Views: 33

Answers (1)

user229044
user229044

Reputation: 239260

Because user is semantically meaningless, while creator is not.

As an example lets assume I have two models called a Book and Person. I can set up an association to a book's author in the people table, and it will be an instance of Person. I can call the association "person", simply because that matches the name of the model, or call it "author". Which one more accurately conveys the relationship between models?

Upvotes: 2

Related Questions