Rails夹具关系不存在

时间:2014-05-12 14:43:52

标签: ruby-on-rails ruby-on-rails-4

Rails 4.1 app。我能够解决它,但对它为什么不起作用感兴趣:

class NotificationSettings < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_one :notification_settings
end

这不起作用:

# test/fixtures/notification_settings.yml

wilmas_notification_settings:
  user: wilma
  checkin_comments: false
  checkin_comments_schedule: instant

这项工作:

# test/fixtures/notification_settings.yml

wilmas_notification_settings:
  user_id: wilma
  checkin_comments: false
  checkin_comments_schedule: instant

所有事情都指向belongs_to的问题,但我感到困惑。

错误是:

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column "user" of relation "notification_settings" does not exist
LINE 1: INSERT INTO "notification_settings" ("user", "checkin_commen...
                                             ^
: INSERT INTO "notification_settings" ("user", "checkin_comments", "checkin_comments_schedule") VALUES (550831404, 'f', 'instant')

1 个答案:

答案 0 :(得分:5)

原来notification_settings的多元化正在抛弃它。这修好了它:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable %w(notification_settings)
end