简体   繁体   English

rails belongs_to customization

[英]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 ? 为什么不简单地在模型中(和表中)声明user_id ,然后只使用belongs_to :user Btw, is now creator an alias to creator_id ? 顺便说一下,现在creatorcreator_id的别名?

Because user is semantically meaningless, while creator is not. 因为user在语义上没有意义,而creator则没有。

As an example lets assume I have two models called a Book and Person . 举个例子,假设我有两个名为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 . 我可以在people表中建立与书籍作者的关联,它将是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? 哪一个更准确地传达了模型之间的关系?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM