has_many关联的未定义方法

时间:2015-01-19 20:35:22

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

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

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

这是Recipe模型代码:

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

  belongs_to :user

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

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

1 个答案:

答案 0 :(得分:0)

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

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并已保存。

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