2
我升级轨2.3应用轨3.0.3升级到Rails 3中,联想的has_many扩展不再与范围的工作
我有我的Product
模型下面的联想扩展和范围
has_many :related_products, :through => :product_related_products do
[:alternative, :complement, :bigger_pack, :higher_quantity, :lower_quality].each do |meth|
define_method(meth) {
find :all, :conditions => ["product_related_products.#{meth} = ?", true] }
end
end
scope :visible, where(:hidden => false)
取自概念:http://guides.rubyonrails.org/association_basics.html#association-extensions
当我调用链关联
@product.related_products.visible.alternative
它可以在导轨2.3罚款,我得到了在Rails 3的以下错误:
undefined method `alternative' for #<ActiveRecord::Relation:0x1047ef978>
activerecord (3.0.3) lib/active_record/relation.rb:371:in `method_missing'
app/views/products/show.html.haml:18:in `_app_views_products_show_html_haml___1131941434_2185376080_0'
我认为它是与所创建的新关系,但林不知道如何着手,轨道导向仍然表明这种方法是可以的。
//编辑后由弗朗索瓦修改建议:是
类定义如下:
class Product < ActiveRecord::Base
has_many :product_related_products
has_many :related_products, :through => :product_related_products
end
class ProductRelatedProduct < ActiveRecord::Base
belongs_to :product
belongs_to :related_product, :class_name => "Product"
scope :with_attribute, lambda {|attr| where(attr, true)}
end
@product.related_products.with_attribute(:alternative) raises:
NoMethodError Exception: undefined method `with_attribute' for #<Class:0x108fbd8b8>
感谢您的回复,但是,这两种解决方案都无效。 – Rob 2010-11-29 10:11:55