Created
April 17, 2009 18:55
-
-
Save knowtheory/97195 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >> Pet.first(:name=>"Bo") | |
| => #<Pet id=1 name="Bo" kind="Portugese Water Dog"> | |
| >> toys = Toy.all | |
| => [#<Toy id=1 name="Chew Toy" price=nil>, #<Toy id=2 name="Squeaky Toy" price=nil>, #<Toy id=3 name="Bell Ball" price=nil>] | |
| >> bo.toys = toys | |
| => [#<Toy id=1 name="Chew Toy" price=nil>, #<Toy id=2 name="Squeaky Toy" price=nil>, #<Toy id=3 name="Bell Ball" price=nil>] | |
| >> bo.save | |
| MysqlError: (mysql_errno=1062, sql_state=23000) Duplicate entry '1-1' for key 'PRIMARY' | |
| Query: INSERT INTO `pets_toys` (`pet_id`, `toy_id`) VALUES (1, 1) | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:92:in `execute_non_query' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:92:in `execute' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:173:in `with_connection' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:90:in `execute' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:26:in `create' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:15:in `each' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:15:in `create' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/repository.rb:51:in `create' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/resource.rb:589:in `hookable__create_nan_before_advised' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:299:in `create' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in `catch' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in `create' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/resource.rb:295:in `hookable__save_nan_before_advised' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:299:in `save' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in `catch' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in `save' | |
| ... 6 levels... | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/support/kernel.rb:6:in `repository' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/associations/relationship.rb:172:in `with_repository' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/associations/one_to_many.rb:298:in `save_resource' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/associations/one_to_many.rb:210:in `save' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/lazy_array.rb:462:in `each' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/lazy_array.rb:462:in `each' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/associations/one_to_many.rb:309:in `send' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/associations/one_to_many.rb:309:in `method_missing' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/associations/one_to_many.rb:210:in `save' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/resource.rb:301:in `hookable__save_nan_before_advised' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/resource.rb:301:in `each' | |
| from /Library/Ruby/Gems/1.8/gems/dm-core-0.9.11/lib/dm-core/resource.rb:301:in `hookable__save_nan_before_advised' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:299:in `save' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in `catch' | |
| from /Library/Ruby/Gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in `save' | |
| from (irb):17>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Extlib::Inflection.plural_word("human","humans") | |
| class Human | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :first_name, String | |
| property :middle_name, String | |
| property :last_name, String | |
| has n, :ownerships, :child_key => [:human_id] | |
| has n, :pets, :through => :ownerships, :child_key => [:pet_id] | |
| end | |
| class Ownership | |
| include DataMapper::Resource | |
| property :human_id, Integer, :key => true | |
| property :pet_id, Integer, :key => true | |
| belongs_to :pet, :child_key => [:pet_id] | |
| belongs_to :human, :child_key => [:human_id] | |
| end | |
| class Pet | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String | |
| property :kind, String | |
| has n, :ownerships, :child_key => [:pet_id] | |
| has n, :humans, :through => :ownerships, :child_key => [:human_id] | |
| has n, :toys, :through => Resource | |
| end | |
| class Toy | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String, :length => 255 | |
| property :price, Integer | |
| has n, :pets, :through => Resource | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'spec' | |
| require 'dm-core' | |
| require 'dm-aggregates' | |
| require 'dm-types' | |
| require 'many_through_models' | |
| DataMapper.setup(:default, "mysql://localhost/dm_pets") | |
| DataMapper.auto_migrate! | |
| describe "behavior i expect out of an orm" do | |
| before(:all) do | |
| # Human.create( :first_name=>,:last_name=>) | |
| @barack = Human.create( :first_name=>"Barack", :last_name=>"Obama") | |
| @michelle = Human.create( :first_name=>"Michelle",:last_name=>"Obama") | |
| @malia = Human.create( :first_name=>"Malia",:last_name=>"Obama") | |
| @sasha = Human.create( :first_name=>"Sasha",:last_name=>"Obama") | |
| @bo = Pet.create( :name => "Bo", :kind=>"Portugese Water Dog") | |
| @gwb = Human.create( :first_name=>"George", :middle_name=>"Walker",:last_name=>"Bush") | |
| @laura = Human.create( :first_name=>"Laura",:last_name=>"Bush") | |
| @barney = Pet.create( :name => "Barney", :kind => "Scottish Terrier Dog") | |
| @beazly = Pet.create( :name => "Miss Beazley", :kind => "Scottish Terrier Dog") | |
| @bill = Human.create( :first_name=>"Bill",:last_name=>"Clinton") | |
| @hillary = Human.create( :first_name=>"Hillary",:last_name=>"Clinton") | |
| @chelsey = Human.create( :first_name=>"Chelsey",:last_name=>"Clinton") | |
| @socks = Pet.create( :name => "Socks", :kind=> "American Shorthair Cat") | |
| @buddy = Pet.create( :name => "Buddy", :kind=> "Chocolate Labrador Retreiver Dog") | |
| @seamus = Pet.create( :name => "Seamus", :kind=> "Labrador Retreiver Dog") | |
| [@barack, @michelle, @malia, @sasha].each{ |p| Ownership.create(:human_id => p.id, :pet_id => @bo.id) } | |
| [@gwb, @laura].each{ |p| Ownership.create( :human_id => p.id, :pet_id => @barney.id) } | |
| [@bill,@hillary, @chelsey].each do |p| | |
| Ownership.create( :human_id => p.id, :pet_id => @socks.id) | |
| Ownership.create( :human_id => p.id, :pet_id => @buddy.id) | |
| Ownership.create( :human_id => p.id, :pet_id => @seamus.id) | |
| end | |
| end | |
| it "should have an accessor for relationships" do | |
| @barack = Human.get(1) | |
| @barack.should respond_to(:ownerships) | |
| @barack.should respond_to(:pets) | |
| end | |
| it "should have at least one pet" do | |
| Human.all.pets.size.should > 1 | |
| end | |
| it "should have finders which return on in property models" do | |
| Human.all(:last_name=>"Bush").size.should == 2 | |
| Human.all(:middle_name.not => nil).should_not == [] | |
| end | |
| it "should be able to find things by properties on immediate child models" do | |
| Human.all("ownerships.pet_id"=>Pet.first(:name=>"Bo").id).should == Human.all(:last_name => "Obama") | |
| end | |
| it "should be able to find things by properties on distant child models" do | |
| Human.all("ownerships.pet.name" => "Bo").should == Human.all(:last_name => "Obama") | |
| Human.all("ownerships.pet.name" => ["Socks","Buddy"]).should == Human.all(:last_name => "Clinton") | |
| end | |
| =begin | |
| it "should let you skip the near model (i.e. join table) when specifying a assocated model to query on" do | |
| Human.all("pets.name" => "Bo").should == Human.all(:last_name => "Obama") | |
| Human.all("pets.name" => ["Socks","Buddy"]).should == Human.all(:last_name => "Clinton") | |
| end | |
| =end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment