Rails 4.一个模型中的两个外键

时间:2014-07-31 07:36:07

标签: ruby-on-rails ruby-on-rails-4 relational-database models

我有两种模式:

#Account migration

create_table :accounts do |t|
  t.string :email
  t.string :password

  t.timestamps
end

#ProjectInvitations migration

create_table :project_invitations do |t|t
  t.integer :sender_account_id #belongs_to :account
  t.string :recipient_first_name
  t.string :recipient_last_name
  t.string :recipient_email_string
  t.integer :recipient_account_id #belongs_to :account
  t.string :status

  t.timestamps
end

如何通过外键sender_account_idrecipient_account_id向模型帐户添加关系?

2 个答案:

答案 0 :(得分:4)

class ProjectInvitation < ActiveRecord::Base

  belongs_to :sender,    class_name: Account, foreign_key: :sender_account_id
  belongs_to :recipient, class_name: Account, foreign_key: :recipient_account_id
end

class Account < ActiveRecord::Base
  has_many: :sent_invitations,     class_name: ProjectInvitation, foreign_key: :sender_account_id
  has_many: :received_invitations, class_name: ProjectInvitation, foreign_key: :recipient_account_id
end

答案 1 :(得分:0)

此代码对我有用:

class ProjectInvitation < ActiveRecord::Base

  belongs_to :sender,    :class_name => Account, :foreign_key => :sender_account_id
  belongs_to :recipient, :class_name => Account, :foreign_key => :recipient_account_id
end

class Account < ActiveRecord::Base
  has_many: :sent_invitations,     class_name: ProjectInvitation, foreign_key: :sender_account_id
  has_many: :received_invitations, class_name: ProjectInvitation, foreign_key: :recipient_account_id
end

控制台测试日志:

2.1.2 :001 > a = Account.create email:"test"
   (0.3ms)  SAVEPOINT active_record_1
  SQL (0.9ms)  INSERT INTO "accounts" ("created_at", "email", "id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", "2014-07-31 08:34:26.767247"], ["email", "test"], ["id", "7d73c3c0-1e59-4988-a589-ee56223be88b"], ["updated_at", "2014-07-31 08:34:26.767247"]]
   (0.2ms)  RELEASE SAVEPOINT active_record_1
 => #<Account id: #<UUID:0x3ffb32085120 UUID:7d73c3c0-1e59-4988-a589-ee56223be88b>, user_id: nil, email: "test", password: nil, created_at: "2014-07-31 08:34:26", updated_at: "2014-07-31 08:34:26">
2.1.2 :002 > i = ProjectInvitation.create status:'pending'
   (0.2ms)  SAVEPOINT active_record_1
  SQL (0.3ms)  INSERT INTO "project_invitations" ("created_at", "id", "status", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", "2014-07-31 08:35:02.956242"], ["id", "7bbfe6b5-fffa-46e0-b92c-a2d0d7aa0469"], ["status", "pending"], ["updated_at", "2014-07-31 08:35:02.956242"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
 => #<ProjectInvitation id: #<UUID:0x3ffb31fab678 UUID:7bbfe6b5-fffa-46e0-b92c-a2d0d7aa0469>, project_id: nil, sender_account_id: nil, recipient_first_name: nil, recipient_last_name: nil, recipient_email_string: nil, recipient_account_id: nil, status: "pending", created_at: "2014-07-31 08:35:02", updated_at: "2014-07-31 08:35:02">
2.1.2 :003 > i.sender = a
 => #<Account id: #<UUID:0x3ffb32085120 UUID:7d73c3c0-1e59-4988-a589-ee56223be88b>, user_id: nil, email: "test", password: nil, created_at: "2014-07-31 08:34:26", updated_at: "2014-07-31 08:34:26">
2.1.2 :004 > i.save
   (0.3ms)  SAVEPOINT active_record_1
  SQL (3.1ms)  UPDATE "project_invitations" SET "sender_account_id" = $1, "updated_at" = $2 WHERE "project_invitations"."id" = '7bbfe6b5-fffa-46e0-b92c-a2d0d7aa0469'  [["sender_account_id", "7d73c3c0-1e59-4988-a589-ee56223be88b"], ["updated_at", "2014-07-31 08:35:30.298393"]]
   (0.2ms)  RELEASE SAVEPOINT active_record_1
 => true
2.1.2 :005 > a.sent_invitations
  ProjectInvitation Load (0.3ms)  SELECT "project_invitations".* FROM "project_invitations"  WHERE "project_invitations"."sender_account_id" = $1  [["sender_account_id", "7d73c3c0-1e59-4988-a589-ee56223be88b"]]
 => #<ActiveRecord::Associations::CollectionProxy [#<ProjectInvitation id: #<UUID:0x3ffb3371dc84 UUID:7bbfe6b5-fffa-46e0-b92c-a2d0d7aa0469>, project_id: nil, sender_account_id: #<UUID:0x3ffb3371d5cc UUID:7d73c3c0-1e59-4988-a589-ee56223be88b>, recipient_first_name: nil, recipient_last_name: nil, recipient_email_string: nil, recipient_account_id: nil, status: "pending", created_at: "2014-07-31 08:35:02", updated_at: "2014-07-31 08:35:30">]>