Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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 通过关联,shell中rails控制台中的“无方法错误”,NoMethodError:undefined Method for_Ruby On Rails_Ruby_Ruby On Rails 3_Postgresql_Sorting - Fatal编程技术网

Ruby on rails 通过关联,shell中rails控制台中的“无方法错误”,NoMethodError:undefined Method for

Ruby on rails 通过关联,shell中rails控制台中的“无方法错误”,NoMethodError:undefined Method for,ruby-on-rails,ruby,ruby-on-rails-3,postgresql,sorting,Ruby On Rails,Ruby,Ruby On Rails 3,Postgresql,Sorting,我正在开发rails 3应用程序中的一个功能。我有一个新的型号,叫做Box,它有很多产品。很多人给我带来了麻烦。所以我做了一个叫BoxProduct的新模型。看起来是这样的: class Box < ActiveRecord::Base attr_accessible :name, :product_ids has_many :box_products, :class_name => 'BoxProduct' has_many :products, t

我正在开发rails 3应用程序中的一个功能。我有一个新的型号,叫做Box,它有很多产品。很多人给我带来了麻烦。所以我做了一个叫BoxProduct的新模型。看起来是这样的:

    class Box < ActiveRecord::Base
    attr_accessible :name, :product_ids
    has_many :box_products, :class_name => 'BoxProduct'
    has_many :products, through: :box_products
    accepts_nested_attributes_for :box_products
    end


    class BoxProduct < ActiveRecord::Base
    attr_accessible :box, :product
    belongs_to :box
    belongs_to :product
    end

    class Product < ActiveRecord::Base
    include Concerns::Notifiable
    include ThinkingSphinx::Scopes

    attr_accessible :box_ids


    has_many :box_products
    has_many :boxes, through: :box_products
    end
-我在这里遇到的第一个问题是:当我访问rails控制台中的一个框时:

[1] pry(main)> b = Box.first
Box Load (2.0ms)  SELECT "boxes".* FROM "boxes" LIMIT 1
=> #<Box id: 1, name: "Test", image: nil, ....                        
[2] pry(main)> b.products
Product Load (1.6ms)  SELECT "products".* FROM "products" INNER JOIN
"box_products" ON "products"."id" = "box_products"."product_id"
WHERE "box_products"."box_id" = 1
=> []
[3] pry(main)> b = Box.where("id" => 2)
Box Load (0.8ms)  SELECT "boxes".* FROM "boxes" WHERE   
"boxes"."id" = 2
=> [#<Box id: 2, name: "Yoo", image: "image.jpeg" ....
[4] pry(main)> b.products
NoMethodError: undefined method `products' for # 
<ActiveRecord::Relation:0x007f3b68c30208>
from /home/vagrant/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:45:in `method_missing'

但是,在CMS中的框视图页面中,@box.products将表中每个框的产品作为工具返回给我迄今为止创建的所有框条目。

您的第一个问题很容易解决:请注意,where不会从数据库返回单个对象,即使只找到一个条目。其中返回一个,它在问题的上下文中充当列表

Box.where("id" => 2)
#=> [#<Box id: 2, ... # Note the `[` at the beginning of the line
或者,您可以使用只返回单个元素的方法:

Box.find(2).products
Box.find_by(id: 2).products
Box.where(id: 2).take.products
Box.where(id: 2).first.products

再次感谢spickermann,您的解释为我指明了正确的方向,让我回顾诸如blocs、symbol to proc和特定于rails的“魔力”之类的东西
Box.find(2).products
Box.find_by(id: 2).products
Box.where(id: 2).take.products
Box.where(id: 2).first.products