Created
February 3, 2010 00:11
-
-
Save gabehollombe/293193 to your computer and use it in GitHub Desktop.
ActiveRecord doesnt like foreign keys?
This file contains 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 Category < ActiveRecord::Base | |
has_many :assignments, :foreign_key => 'category_uuid', :primary_key => 'uuid' | |
end | |
class Post < ActiveRecord::Base | |
has_many :assignments, :foreign_key => 'post_uuid', :primary_key => 'uuid' | |
end | |
class Assignment < ActiveRecord::Base | |
belongs_to :post, :foreign_key => 'uuid', :primary_key => 'post_uuid' | |
belongs_to :category, :foreign_key => 'uuid', :primary_key => 'category_uuid' | |
end | |
Example console session: | |
>> p = Post.create | |
Post Create (0.5ms) INSERT INTO "posts" ("name", "created_at", "uuid", "updated_at") VALUES(NULL, '2010-02-03 00:09:29', NULL, '2010-02-03 00:09:29') | |
=> #<Post id: 1, uuid: nil, name: nil, created_at: "2010-02-03 00:09:29", updated_at: "2010-02-03 00:09:29"> | |
>> p.uuid = 'abc' | |
=> "abc" | |
>> p.save | |
Post Update (0.8ms) UPDATE "posts" SET "updated_at" = '2010-02-03 00:09:35', "uuid" = 'abc' WHERE "id" = 1 | |
=> true | |
>> c = Category.create | |
Category Create (0.7ms) INSERT INTO "categories" ("name", "created_at", "uuid", "updated_at") VALUES(NULL, '2010-02-03 00:09:40', NULL, '2010-02-03 00:09:40') | |
=> #<Category id: 1, name: nil, uuid: nil, created_at: "2010-02-03 00:09:40", updated_at: "2010-02-03 00:09:40"> | |
>> c.uuid = 'def' | |
=> "def" | |
>> c.save | |
Category Update (0.7ms) UPDATE "categories" SET "updated_at" = '2010-02-03 00:09:49', "uuid" = 'def' WHERE "id" = 1 | |
=> true | |
>> a = Assignment.create | |
Assignment Create (0.4ms) INSERT INTO "assignments" ("created_at", "updated_at", "post_uuid", "category_uuid") VALUES('2010-02-03 00:09:53', '2010-02-03 00:09:53', NULL, NULL) | |
=> #<Assignment id: 1, post_uuid: nil, category_uuid: nil, created_at: "2010-02-03 00:09:53", updated_at: "2010-02-03 00:09:53"> | |
>> a.post_uuid = 'abc' | |
=> "abc" | |
>> a.category_uuid = 'def' | |
=> "def" | |
>> a.save | |
Assignment Update (0.7ms) UPDATE "assignments" SET "updated_at" = '2010-02-03 00:10:06', "post_uuid" = 'abc', "category_uuid" = 'def' WHERE "id" = 1 | |
=> true | |
>> a.reload | |
Assignment Load (0.4ms) SELECT * FROM "assignments" WHERE ("assignments"."id" = 1) | |
=> #<Assignment id: 1, post_uuid: "abc", category_uuid: "def", created_at: "2010-02-03 00:09:53", updated_at: "2010-02-03 00:10:06"> | |
>> a.post | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment