Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
即帖子模型有一个列poster_id,对于海报我实际上使用的是用户模型。显然这是一个 User has_many Posts 关系,但是如何指定 poster_id 实际上是 User 模型的 id 呢?
使用 foreign_key 选项:
has_many :posts, :foreign_key => :poster_id
对于 Post 模型,它将是
belongs_to :user, :foreign_key => :poster_id
或者
belongs_to :poster, :class_name => 'User'