Skip to content

Instantly share code, notes, and snippets.

@teamon
Created August 5, 2008 19:11
Show Gist options
  • Save teamon/4108 to your computer and use it in GitHub Desktop.
Save teamon/4108 to your computer and use it in GitHub Desktop.
>> p = Product.first
=> #<Product id=1 type=:product name="X wing 100" description=<not loaded> testers_left=0 created_at=#<DateTime: 23564969261/9600,1/12,2299161> updated_at=#<DateTime: 23564969261/9600,1/12,2299161> deleted_at=<not loaded> owner_id=2>
>> p.testers
=> []
>> p.testers.count
=> 0
>> p.save
DataMapper::Associations::ImmutableAssociationError: You can not modify this association
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.4/lib/dm-core/associations/one_to_many.rb:247:in `assert_mutable'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.4/lib/dm-core/associations/one_to_many.rb:199:in `save'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.4/lib/dm-core/resource.rb:273:in `hookable__save_nan_before_advised'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.4/lib/dm-core/resource.rb:273:in `each'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.4/lib/dm-core/resource.rb:273:in `hookable__save_nan_before_advised'
from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.4/lib/extlib/hook.rb:294:in `save!'
from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.4/lib/extlib/hook.rb:292:in `catch'
from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.4/lib/extlib/hook.rb:292:in `save!'
from /opt/local/lib/ruby/gems/1.8/gems/dm-validations-0.9.4/lib/dm-validations.rb:62:in `save'
from (irb):20
>>
class Product
include DataMapper::Resource
property :id, Integer, :serial => true
property :type, Enum[:product, :service] #, :set => [:product, :service]
property :name, String, :nullable => false, :format => /\A[\w ]+\z/u
property :description, Text, :nullable => false
property :testers_left, Integer, :default => 0
property :created_at, DateTime
property :updated_at, DateTime
property :deleted_at, ParanoidDateTime
validates_with_block :category do
unless category.nil?
category.children.count == 0 || [false, 'Please choose lowest level category'.t]
else
[false, 'Please choose category'.t]
end
end
validates_with_block :owner do
unless owner.nil?
owner.is_a?(Company) || [false, 'Only company can has products'.t]
else
[false, 'Owner is required'.t]
end
end
belongs_to :category
belongs_to :owner, :class_name => User, :child_key => [:owner_id]
has 1, :button
has n, :opinions
has n, :product_testers
has n, :testers, :through => :product_testers, :class_name => User
end
class ProductTester
include DataMapper::Resource
property :id, Integer, :serial => true
property :status, Enum[:waiting], :default => :waiting
property :created_at, DateTime
belongs_to :tester, :class_name => User
belongs_to :product
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment