Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails多对多关联产生错误:NoMethodError:undefined method`product';对于#<;ChoiceProductLink:0x0000106838310>;_Ruby On Rails_Ruby_Has Many Through - Fatal编程技术网

Ruby on rails Rails多对多关联产生错误:NoMethodError:undefined method`product';对于#<;ChoiceProductLink:0x0000106838310>;

Ruby on rails Rails多对多关联产生错误:NoMethodError:undefined method`product';对于#<;ChoiceProductLink:0x0000106838310>;,ruby-on-rails,ruby,has-many-through,Ruby On Rails,Ruby,Has Many Through,我按照本教程添加了多对多关联: https://www.youtube.com/watch?v=ZJXHmesqJr0&index=35&list=PLvG9ngQqIEISob71DI7Id68lXLQ4hVoVH 我的目标是创建一个products表、一个choices表以及这些表之间的连接(多对多)。在这些连接上,我需要存储价格等信息 这是我的密码 Schema.rb: ActiveRecord::Schema.define(version: 20141213005617

我按照本教程添加了多对多关联:

https://www.youtube.com/watch?v=ZJXHmesqJr0&index=35&list=PLvG9ngQqIEISob71DI7Id68lXLQ4hVoVH
我的目标是创建一个products表、一个choices表以及这些表之间的连接(多对多)。在这些连接上,我需要存储价格等信息

这是我的密码

Schema.rb:

ActiveRecord::Schema.define(version: 20141213005617) do

    create_table "choice_product_links", force: true do |t|
      t.integer  "price"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.integer  "choice_id"
      t.integer  "product_id"
    end

    create_table "choices", force: true do |t|
      t.string   "name"
      t.decimal  "price"
      t.text     "description"
      t.datetime "created_at"
      t.datetime "updated_at"
    end

    create_table "products", force: true do |t|
      t.string   "name"
      t.datetime "created_at"
      t.datetime "updated_at"
    end

  end
模型包括:

  class Choice < ActiveRecord::Base
    has_many :choice_product_links
    has_many :products, :through => :choice_product_links
  end


class ChoiceProductLink < ActiveRecord::Base
      belongs_to :product
      belongs_to :choice
    end

 class Product < ActiveRecord::Base
      has_many :choice_product_links
      has_many :choices, :through => :choice_product_links
    end
类选择:选择\u产品\u链接
结束
类ChoiceProductLink:选择\u产品\u链接
结束
当我使用控制台测试代码时,我输入一个产品p1(工作正常),一个选项c1(工作正常)。执行以下操作时会出现问题:

>> cpl1 = ChoiceProductLink.new
#<ChoiceProductLink id: nil, price: nil, created_at: nil, updated_at: nil, choice_id: nil,   product_id: nil>

>> cpl1.save
true

>> cpl1.product = p1
NoMethodError: undefined method `product=' for #<ChoiceProductLink:0x00000101f31bf8>
>cpl1=ChoiceProductLink.new
#
>>cpl1.save
真的
>>cpl1.product=p1
NoMethodError:for的未定义方法“product=”#
有人能帮我吗?
谢谢大家

我之前的回答是错的——对不起。我不完全确定你的模型为什么不能正常工作。我刚刚设置了一个测试Rails应用程序,并以相同的迁移和类结构运行了您运行的所有代码,结果如下:

Joshuas-MacBook-Pro:models joshua$ rails console
Loading development environment (Rails 4.0.0)
irb(main):001:0> cpl1 = ChoiceProductLink.new
=> #<ChoiceProductLink id: nil, price: nil, created_at: nil, updated_at: nil, choice_id: nil, product_id: nil>
irb(main):002:0> cpl1.save
   (0.1ms)  begin transaction
  SQL (7.0ms)  INSERT INTO "choice_product_links" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", Sat, 13 Dec 2014 03:12:50 UTC +00:00], ["updated_at", Sat, 13 Dec 2014 03:12:50 UTC +00:00]]
   (0.7ms)  commit transaction
=> true
irb(main):004:0* p1 = Product.new
=> #<Product id: nil, name: nil, created_at: nil, updated_at: nil>
irb(main):005:0> p1.save
   (0.1ms)  begin transaction
  SQL (1.5ms)  INSERT INTO "products" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", Sat, 13 Dec 2014 03:13:25 UTC +00:00], ["updated_at", Sat, 13 Dec 2014 03:13:25 UTC +00:00]]
   (2.5ms)  commit transaction
=> true
irb(main):006:0> cpl1.product = p1
=> #<Product id: 1, name: nil, created_at: "2014-12-13 03:13:25", updated_at: "2014-12-13 03:13:25">
irb(main):007:0> cpl1.save
   (0.1ms)  begin transaction
  SQL (0.7ms)  UPDATE "choice_product_links" SET "product_id" = ?, "updated_at" = ? WHERE "choice_product_links"."id" = 1  [["product_id", 1], ["updated_at", Sat, 13 Dec 2014 03:13:35 UTC +00:00]]
   (1.6ms)  commit transaction
=> true
Joshuas MacBook Pro:型号joshua$rails控制台
加载开发环境(Rails 4.0.0)
irb(主):001:0>cpl1=ChoiceProductLink.new
=> #
irb(主):002:0>cpl1.save
(0.1ms)开始事务处理
SQL(7.0ms)插入“选择产品链接”(“创建时间”,“更新时间”)值(?,[“创建时间”,Sat,2014年12月13日03:12:50 UTC+00:00],“更新时间”,Sat,2014年12月13日03:12:50 UTC+00:00]]
(0.7ms)提交事务
=>正确
irb(主):004:0*p1=新产品
=> #
irb(主):005:0>p1.0保存
(0.1ms)开始事务处理
SQL(1.5ms)插入“产品”(“创建时间”、“更新时间”)值(?,)[“创建时间”,Sat,2014年12月13日03:13:25 UTC+00:00],“更新时间”,Sat,2014年12月13日03:13:25 UTC+00:00]]
(2.5ms)提交事务
=>正确
irb(主):006:0>cpl1.product=p1
=> #
irb(主):007:0>cpl1.save
(0.1ms)开始事务处理
SQL(0.7ms)更新“选择产品链接”设置“产品id”=?,“更新位置”=?其中“选择产品链接”。“id”=1[[“产品id”,1],“更新时间”,Sat,2014年12月13日03:13:35 UTC+00:00]]
(1.6ms)提交事务
=>正确

这和你做的相似吗?据我所知,它是有效的…

解决方案是创建一个迁移文件,其中包含以下代码:

class CreateConstraints2 < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.timestamps
    end

    create_table :choices do |t|
      t.string :name
      t.timestamps
    end

    create_table : choice_product_links do |t|
      t.belongs_to :product
      t.belongs_to :choice
      t.datetime : choice_product_links
      t.timestamps
    end
  end
end
class CreateConstraints2
有了这个迁移文件,一切都可以完美地工作。我之前所做的是使用单独的迁移文件创建表,然后添加“has_many”和“belish_to”


我不知道为什么会有不同,但这就是解决办法。我来回试了将近100次:p

我必须使用“外键”吗?我想你粘贴错了。。。您在问题中添加了Youtube链接。:-)@JoshuaSmock这是一个5分钟的youtube教程,由CodeAcademy提供如何处理多对多的问题。因此,该链接是正确的:p@JoshuaSmock你能帮助我吗?或者请投赞成票,因为nobidy帮助我:p@BenSpi坦率地说,我看不出有什么理由会导致那样的错误。从技术上讲,没有什么能阻止您像现在这样在联接模型上手动分配关联。约书亚是正确的,因为你通常不会这样做,但从技术上讲,这是合法的;
属于:product
隐式创建一个
product=
方法。Rails不“知道”这是一个连接模型,它与任何其他活动记录模型都没有区别。@PaulRichter你认为我的代码有什么问题?@PaulRichter哦,哦,你是对的,我误解了这个问题,对我的Rails foo也有点生疏…@JoshuaSmock非常感谢你的帮助!看看我解决问题的答案。你知道我干了什么吗?我很困惑。所以之前创建表时有单独的迁移文件。这些迁移文件是否创建了正确的
product\u id
choice\u id
列?@PaulRichter也许这是我的错误:我创建了一个表ChoiceProductLink,其中包含两列:product\u id和choice\u id硬编码。我认为由于名称约定,rails将创建所需的方法。但这看起来并不重要,如果这是一个极其愚蠢的错误,请不要杀了我:)我认为这不重要,因为Rails创建的迁移(以及在您的OP中出现的
schema.rb
)是有效的,关联是使用模型而不是迁移建立的。我逐字复制了您发布的内容,并在测试时进行了迁移。¯_(ツ)_/“好吧,这与我在对约书亚回答的评论中所说的有关。不是列名创造了所需的方法,而是<代码>属于<代码>并且<代码>在模型中有许多<代码>。你是对的,这些列必须符合命名约定,这在你的情况下是完全正确的。如果它怀疑,就把地球夷为平地。”重新开始!(这不是严肃的建议)