轨道排除模型关系中"prefix"的名称



假设我有模型ArticleArticleVoteArticleComment

有没有办法直接删除关系中的前缀,如article_(而不是设置class_nameforeign_key等(?

class Article
belongs_to :user
has_many :article_votes # Just call this "votes"
has_many :article_comments # Just call this "comments"
end

不,没有办法。

您可以指定所需的任何名称,但当无法从关联名称(可能还有外键,具体取决于您的命名方式(推断出类名时,需要提供类名:

class Article
belongs_to :user
has_many :votes, class_name: 'ArticleVote'
has_many :comments, class_name: 'ArticleComment'
end

我不明白这应该以任何其他方式实现。rails 如何知道您希望以另一种方式为这个特定关联构建名称?

最新更新