【问题标题】:undefined method with has_many association具有 has_many 关联的未定义方法
【发布时间】:2015-03-17 22:56:44
【问题描述】:

我有 2 个具有一对多关联的模型:用户和食谱。用户类 has_many :recipes 而配方类属于_to :user。我已经运行了迁移,重新加载了 rails 控制台,并检查以确保 user_id 是 recipes 表中的一列。不过,当我尝试将配方附加到用户时,我得到一个未定义的方法错误:

2.0.0-p598 :047 > user.recipes << Recipe.first
NoMethodError: undefined method `recipes' for #<User:0x00000004326fa0>

这里是迁移代码(我已经运行了 rake db:migrate):

class AddUserIdToRecipes < ActiveRecord::Migration
  def change
    add_column :recipes, :user_id, :integer
  end
end

这是用户模型代码:

class User < ActiveRecord::Base
  has_one :profile
  has_many :recipes
end

这是配方模型代码:

class Recipe < ActiveRecord::Base
  validates_presence_of :title, :body

  belongs_to :user

  def long_title
    "#{title} - #{published_at}"
  end
end

为什么食谱仍然显示为未定义的方法?

【问题讨论】:

  • "您是否尝试将其关闭再打开?" (谈论你的 Rails 服务器)
  • 在您的迁移中,尝试将 ':user_id, :integer' 添加到 recipes 下的另一列,并从 recipe 列中删除 :user_id, :integer。重置迁移并再次 rake。
  • 谢谢,这有效(打开和关闭 Rails 服务器)。尽管如此,rails 服务器与任何这些都有什么关系,仍然有点困惑。我所做的只是编辑数据库,我在没有启动 Rails 服务器的情况下就看到了变化

标签: ruby-on-rails ruby-on-rails-4 associations has-many


【解决方案1】:

在你的控制台上试试这个:

irb(main):007:0> user = User.new first_name: 'John', last_name: 'Doe'
=> #<User id: nil, first_name: "John", last_name: "Doe", created_at: nil, updated_at: nil>
irb(main):008:0> user.save
   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "users" ("created_at", "first_name", "last_name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2015-01-19 21:14:33.489371"], ["first_name", "John"], ["last_name", "Doe"], ["updated_at", "2015-01-19 21:14:33.489371"]]
   (0.6ms)  commit transaction
=> true
irb(main):009:0> r = Recipe.new name: 'oooohh awesome', description: 'my description goes here'
=> #<Recipe id: nil, name: "oooohh awesome", description: "my description goes here", created_at: nil, updated_at: nil, user_id: nil>
irb(main):010:0> r.save
   (0.1ms)  begin transaction
  SQL (0.2ms)  INSERT INTO "recipes" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2015-01-19 21:15:16.548090"], ["description", "my description goes here"], ["name", "oooohh awesome"], ["updated_at", "2015-01-19 21:15:16.548090"]]
   (1.2ms)  commit transaction
=> true
irb(main):011:0> user.recipes << Recipe.first
  Recipe Load (0.2ms)  SELECT  "recipes".* FROM "recipes"   ORDER BY "recipes"."id" ASC LIMIT 1
   (0.0ms)  begin transaction
  SQL (0.2ms)  UPDATE "recipes" SET "updated_at" = ?, "user_id" = ? WHERE "recipes"."id" = 1  [["updated_at", "2015-01-19 21:15:49.181586"], ["user_id", 1]]
   (1.3ms)  commit transaction
  Recipe Load (0.2ms)  SELECT "recipes".* FROM "recipes"  WHERE "recipes"."user_id" = ?  [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Recipe id: 1, name: "oooohh awesome", description: "sper long deskdk", created_at: "2015-01-19 21:10:24", updated_at: "2015-01-19 21:15:49", user_id: 1>]>
irb(main):012:0> user.save
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
=> true
irb(main):013:0> user.recipes
=> #<ActiveRecord::Associations::CollectionProxy [#<Recipe id: 1, name: "oooohh awesome", description: "sper long deskdk", created_at: "2015-01-19 21:10:24", updated_at: "2015-01-19 21:15:49", user_id: 1>]>
irb(main):014:0> user.recipes.first
=> #<Recipe id: 1, name: "oooohh awesome", description: "sper long deskdk", created_at: "2015-01-19 21:10:24", updated_at: "2015-01-19 21:15:49", user_id: 1>
irb(main):015:0> 

您可以看到Recipe.first 已插入user.recipes 并保存。

我制作了两个与您相似的模型,并且与您的设置完全相同。您可以按照上面的代码编写控制器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多