Ruby on rails 3 rails的疯狂通过联想获得了很多好处
我正努力满足自己的需要,但我遇到了麻烦。我会尽可能地解释,不要粘贴太多的代码,因为Spree有很多 首先。我像这样扩展了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])
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