Created
January 24, 2012 08:41
-
-
Save veganstraightedge/1668925 to your computer and use it in GitHub Desktop.
habtm in Rails 3.2
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
class CreateApples < ActiveRecord::Migration | |
def change | |
create_table :apples do |t| | |
t.string :name | |
end | |
end | |
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
class CreateOranges < ActiveRecord::Migration | |
def change | |
create_table :oranges do |t| | |
t.string :name | |
end | |
end | |
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
class CreateApplesOranges < ActiveRecord::Migration | |
def up | |
create_table :apples_oranges, :id => false do |t| | |
t.belongs_to :apple, :orange | |
t.timestamps | |
end | |
end | |
def down | |
drop_table :apples_oranges | |
end | |
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
class Apple < ActiveRecord::Base | |
has_and_belongs_to_many :oranges | |
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
class Orange < ActiveRecord::Base | |
has_and_belongs_to_many :apples | |
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
$: r c | |
Loading development environment (Rails 3.2.0) | |
>> Apple.create(:name => "foo") | |
(0.1ms) begin transaction | |
SQL (0.5ms) INSERT INTO "apples" ("name") VALUES (?) [["name", "foo"]] | |
(2.6ms) commit transaction | |
=> #<Apple id: 1, name: "foo"> | |
>> Orange.create(:name => "bar") | |
(0.1ms) begin transaction | |
SQL (0.4ms) INSERT INTO "oranges" ("name") VALUES (?) [["name", "bar"]] | |
(2.3ms) commit transaction | |
=> #<Orange id: 1, name: "bar"> | |
>> Apple.first.oranges << Orange.first | |
Apple Load (0.2ms) SELECT "apples".* FROM "apples" LIMIT 1 | |
Orange Load (0.2ms) SELECT "oranges".* FROM "oranges" LIMIT 1 | |
(0.0ms) begin transaction | |
(0.2ms) INSERT INTO "apples_oranges" ("orange_id", "apple_id") VALUES (1, 1) | |
SQLite3::ConstraintException: constraint failed: INSERT INTO "apples_oranges" ("orange_id", "apple_id") VALUES (1, 1) | |
(0.1ms) rollback transaction | |
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed: INSERT INTO "apples_oranges" ("orange_id", "apple_id") VALUES (1, 1) | |
from /Library/Ruby/Gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/statement.rb:108:in `step' | |
from /Library/Ruby/Gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/statement.rb:108:in `each' | |
from /Library/Ruby/Gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/statement.rb:107:in `loop' | |
from /Library/Ruby/Gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/statement.rb:107:in `each' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/sqlite_adapter.rb:253:in `to_a' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/sqlite_adapter.rb:253:in `exec_query' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log' | |
from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/sqlite_adapter.rb:247:in `exec_query' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:61:in `exec_insert' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:88:in `insert' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/has_and_belongs_to_many_association.rb:29:in `insert_record' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:492:in `concat_records' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:342:in `add_to_target' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:491:in `concat_records' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:489:in `each' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:489:in `concat_records' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:134:in `concat' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:149:in `transaction' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/transactions.rb:208:in `transaction' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:148:in `transaction' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:134:in `concat' | |
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/associations/collection_proxy.rb:117:in `<<' | |
from (irb):3>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ryanbigg pointed out on Twitter that it's the timestamps on the join table that is tripping it up.
https://twitter.com/#!/ryanbigg/status/161739182664990721