Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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 3 rails的疯狂通过联想获得了很多好处_Ruby On Rails 3_Has Many Through_Spree - Fatal编程技术网

Ruby on rails 3 rails的疯狂通过联想获得了很多好处

Ruby on rails 3 rails的疯狂通过联想获得了很多好处,ruby-on-rails-3,has-many-through,spree,Ruby On Rails 3,Has Many Through,Spree,我正努力满足自己的需要,但我遇到了麻烦。我会尽可能地解释,不要粘贴太多的代码,因为Spree有很多 首先。我像这样扩展了OptionType Spree::OptionType.class_eval do PRODUCT_TYPES = SuperMap.new( [:default, 0], [:vehicle_bonnet, 1], [:vehicle_images, 2])

我正努力满足自己的需要,但我遇到了麻烦。我会尽可能地解释,不要粘贴太多的代码,因为Spree有很多

首先。我像这样扩展了
OptionType

Spree::OptionType.class_eval do
  PRODUCT_TYPES = SuperMap.new( [:default, 0],
                            [:vehicle_bonnet, 1],
                            [:vehicle_images, 2])

  super_mapped_attr :product_type, PRODUCT_TYPES

  attr_accessible :product_type
end
我想在
ProductsController的
show
视图中查看我正在处理的
产品类型,如下所示:

<% if @product.option_types.product_type_key == :vehicle_bonnet %>
   ...
<% end %>
到目前为止似乎还可以

module Spree
  class ProductOptionType < ActiveRecord::Base
    belongs_to :product
    belongs_to :option_type
    acts_as_list :scope => :product
  end
end
这不是最漂亮的方法,但它的效果正如我所期望的

module Spree
  class ProductOptionType < ActiveRecord::Base
    belongs_to :product
    belongs_to :option_type
    acts_as_list :scope => :product
  end
end
module Spree
  class OptionType < ActiveRecord::Base
    has_many :option_values, :order => :position, :dependent => :destroy
    has_many :product_option_types, :dependent => :destroy
    has_and_belongs_to_many :prototypes, :join_table => 'spree_option_types_prototypes'

    attr_accessible :name, :presentation, :option_values_attributes

    validates :name, :presentation, :presence => true
    default_scope :order => "#{self.table_name}.position"

    accepts_nested_attributes_for :option_values, :reject_if => lambda { |ov| ov[:name].blank? || ov[:presentation].blank? }, :allow_destroy => true
  end
end
def type_of_product?(type)
  options.each do |opts| 
    if opts.option_type.product_type_key == type
      return true
    end
  end
  false
end