在 Rails 中建立关联的问题

tfa*_*ina 1 activerecord ruby-on-rails

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

我有这样的迁移:

    create_table :loves do |t|
      t.references :post, null: false, foreign_key: true
      t.references :user, null: false, foreign_key: true
    end
Run Code Online (Sandbox Code Playgroud)

用户名

  has_many :loves
Run Code Online (Sandbox Code Playgroud)

后.rb

  has_many :loves
Run Code Online (Sandbox Code Playgroud)

爱情.rb

class Love < ApplicationRecord
  belongs_to :user
  has_one :dinner
end

Run Code Online (Sandbox Code Playgroud)

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

uninitialized constant User::Lofe 
Run Code Online (Sandbox Code Playgroud)

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

在 Rails 控制台中,如果我输入,User.first.love我会得到一个错误,表明Did you mean? loves这似乎表明关联存在(适用于用户和晚餐),但如果我输入User.first.loves它会抛出相同的未初始化常量错误。

Mur*_*foX 6

我做了一些研究,问题出在 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