在Rails中建立关联的问题



我有一个Post模型和一个User模型。用户可以喜欢帖子,这些信息被保存在一个单独的爱模型中,而不是直接在帖子上,因为用户必须能够看到他们喜欢的帖子的记录。

我有一个这样的迁移:

create_table :loves do |t|
t.references :post, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
end

用户.rb

has_many :loves

Post.rb

has_many :loves

Love.rb

class Love < ApplicationRecord
belongs_to :user
has_one :dinner
end

这种关联似乎不起作用,每当我试图喜欢一个帖子时,我都会收到这样的错误:

uninitialized constant User::Lofe 

我意识到"Lofe"这个词可能看起来像是Love的拼写错误,这就是我的想法,但我已经搜索了整个代码库,没有Lofe的实例。

在Rails控制台中,如果我键入User.first.love,我将得到一个错误,提示Did you mean? loves,这似乎表明关联存在(适用于Users和Dinner(,但如果我随后键入User.first.loves,它将抛出相同的未初始化常量错误。

我做了一些研究,问题出在Rails拐点引擎上。如您所见:https://twitter.com/andypike/status/578214888465657856在这里:https://rails.lighthouseapp.com/projects/8994/tickets/2407-inflector-singularising-loves-to-lofe-but-pluralizing-love-to-loves显然,当使用单词love时,您需要在config/initizers/inflections.rb中手动配置它

最新更新