Skip to content

Instantly share code, notes, and snippets.

@afarnham
Created September 10, 2008 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afarnham/9973 to your computer and use it in GitHub Desktop.
Save afarnham/9973 to your computer and use it in GitHub Desktop.
class Group
include DataMapper::Resource
property :id, Integer, :serial => true
property :name, String, :length => 256
has n, :states, :through => Resource
end
class State
include DataMapper::Resource
property :id, Integer, :serial => true
property :name, String, :length => 256
property :code, String
has n, :groups, :through => Resource
end
-----------------------------------------------------------------
>> g = Group.create(:name => "test")
=> #<Group id=2 name="test">
>> g.states
=> [#<State id=1 name="Puerto Rico" code="PR">]
>> g.states << State.all[1]
=> [#<State id=1 name="Puerto Rico" code="PR">, #<State id=2 name="New Hampshire" code="NH">]
>> g.save
=> true
>> g.states.delete(State.get!(2))
=> #<State id=2 name="New Hampshire" code="NH">
>> g.states
=> [#<State id=1 name="Puerto Rico" code="PR">]
>> g.save
NoMethodError: undefined method `model' for nil:NilClass from /usr/lib/ruby/gems/1.8/gems/dm-core-0.9.5/lib/dm-core/associations/one_to_many.rb:291:in `save_resource'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment