Created
January 27, 2009 18:37
-
-
Save alloy/53464 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
% script/generate model GrandParent name:string | |
exists app/models/ | |
exists test/unit/ | |
exists test/fixtures/ | |
create app/models/grand_parent.rb | |
create test/unit/grand_parent_test.rb | |
create test/fixtures/grand_parents.yml | |
create db/migrate | |
create db/migrate/20090127182616_create_grand_parents.rb | |
% script/generate model Parent name:string grand_parent_id:integer | |
exists app/models/ | |
exists test/unit/ | |
exists test/fixtures/ | |
create app/models/parent.rb | |
create test/unit/parent_test.rb | |
create test/fixtures/parents.yml | |
exists db/migrate | |
create db/migrate/20090127182710_create_parents.rb | |
% script/generate model Child name:string parent_id:integer | |
exists app/models/ | |
exists test/unit/ | |
exists test/fixtures/ | |
create app/models/child.rb | |
create test/unit/child_test.rb | |
create test/fixtures/children.yml | |
exists db/migrate | |
create db/migrate/20090127182723_create_children.rb | |
% cat app/models/grand_parent.rb | |
class GrandParent < ActiveRecord::Base | |
has_many :parents | |
accepts_nested_attributes_for :parents | |
end | |
% cat app/models/parent.rb | |
class Parent < ActiveRecord::Base | |
has_many :children | |
accepts_nested_attributes_for :children | |
end | |
% rake db:migrate | |
(in /Users/eloy/tmp/3-levels) | |
== CreateGrandParents: migrating ============================================= | |
-- create_table(:grand_parents) | |
-> 0.0051s | |
== CreateGrandParents: migrated (0.0056s) ==================================== | |
== CreateParents: migrating ================================================== | |
-- create_table(:parents) | |
-> 0.0028s | |
== CreateParents: migrated (0.0029s) ========================================= | |
== CreateChildren: migrating ================================================= | |
-- create_table(:children) | |
-> 0.0032s | |
== CreateChildren: migrated (0.0033s) ======================================== | |
% sc | |
Loading development environment (Rails 2.3.0) | |
>> GrandParent.count | |
=> 0 | |
>> Parent.count | |
=> 0 | |
>> Child.count | |
=> 0 | |
>> GrandParent.create(:name => 'grand parent', :parents_attributes => { 'new_1' => { :name => 'parent 1', :children_attributes => { 'new_1' => { :name => 'child 1' }, 'new_2' => { :name => 'child 2' } } }, 'new_2' => { :name => 'parent 2', :children_attributes => { 'new_1' => { :name => 'child 1' }, 'new_2' => { :name => 'child 2' } }}}) | |
=> #<GrandParent id: 1, name: "grand parent", created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18"> | |
>> GrandParent.count | |
=> 1 | |
>> Parent.count | |
=> 2 | |
>> Child.count | |
=> 4 | |
>> Parent.all | |
=> [#<Parent id: 1, name: "parent 1", grand_parent_id: 1, created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18">, #<Parent id: 2, name: "parent 2", grand_parent_id: 1, created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18">] | |
>> Child.all | |
=> [#<Child id: 1, name: "child 1", parent_id: 1, created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18">, #<Child id: 2, name: "child 2", parent_id: 1, created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18">, #<Child id: 3, name: "child 1", parent_id: 2, created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18">, #<Child id: 4, name: "child 2", parent_id: 2, created_at: "2009-01-27 18:32:18", updated_at: "2009-01-27 18:32:18">] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment