Skip to content

Instantly share code, notes, and snippets.

@dlt
Created September 3, 2011 17:40
Show Gist options
  • Save dlt/1191511 to your computer and use it in GitHub Desktop.
Save dlt/1191511 to your computer and use it in GitHub Desktop.
nested_attributes
class Product < ActiveRecord::Base
has_many :fields, :as => :attributable, :dependent => :destroy
accepts_nested_attributes_for :fields
end
class Field < ActiveRecord::Base
belongs_to :attributable, :polymorphic => true
belongs_to :column
scope :with_column, select("fields.*, columns.column_name AS 'name'").joins(:column)
validates_presence_of :column_id, :value
end
class Variant < ActiveRecord::Base
belongs_to :store
belongs_to :product
has_many :supply_movements, :dependent => :delete_all
has_many :sale_items
has_many :order_items
has_many :fields, :as => :attributable
accepts_nested_attributes_for :fields, :reject_if => lambda { |a| a.values.all?(&:blank?) }
# validates_presence_of :product
validates_inclusion_of :active, :in => [true, false]
attr_accessible :store_id, :price, :reference, :barcode, :minimum_stock, :stock, :fields_attributes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment