【问题标题】:Has many associations error when not using default column name不使用默认列名时有许多关联错误
【发布时间】:2016-06-17 09:28:38
【问题描述】:

当外键没有默认名称时,我无法建立良好的关联。 我想访问属于一个participant的所有subjects(外键=questioner_id)。

这引发了我一个错误

p = Participant.first
p.subjects

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: subject_participants.participant_id: SELECT "participants".* FROM "participants" INNER JOIN "subject_participants" ON "participants"."id" = "subject_participants"."subject_id" WHERE "subject_participants"."participant_id" = ?

为什么它会寻找 subject_participants.participant_id ?这只是一个has_many关联,我认为这种情况下不应该调用subject_participants表...

interested_idquestioner_id 来自同一个模型,但角色不同。一个必须通过subject_participants table,另一个必须直接进入subjects table

我的模型: 参与者.rb

class Participant < ActiveRecord::Base
 has_many :subjects, foreign_key: "questioner_id", class_name: "Participant" #questioner
 has_many :subjects, through: :subject_participants, foreign_key: "interested", class_name: "Participant" #interested
 has_many :subject_participants
 has_many :conference_participants
 has_many :conferences, through: :conference_participants
end

主题.rb

class Subject < ActiveRecord::Base
 validates_presence_of  :title, :questioner, :conference, :description

 has_many :subject_participants
 has_many :interested, through: :subject_participants, :class_name => "Participant" #interested
 belongs_to :questioner, :class_name => "Participant" 
 belongs_to :conference
end

subject_participant.rb

class SubjectParticipant < ActiveRecord::Base
  validates_presence_of :interested_id, :subject_id
  belongs_to :interested, :class_name => "Participant"
  belongs_to :subject
end

schema.rb

create_table "participants", force: :cascade do |t|
  t.string   "name"
  t.datetime "created_at",                          null: false
  t.datetime "updated_at",                          null: false
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
end

add_index "participants", ["email"], name: "index_participants_on_email", unique: true
add_index "participants", ["reset_password_token"], name: "index_participants_on_reset_password_token", unique: true

create_table "subject_participants", force: :cascade do |t|
  t.integer  "interested_id"
  t.integer  "subject_id"
  t.datetime "created_at",    null: false
  t.datetime "updated_at",    null: false
end

create_table "subjects", force: :cascade do |t|
  t.string   "title",         null: false
  t.text     "description"
  t.integer  "questioner_id", null: false
  t.integer  "conference_id", null: false
  t.datetime "created_at",    null: false
  t.datetime "updated_at",    null: false
end

【问题讨论】:

  • 实际上我想访问属于 questioner_id(参与者类)的所有主题。我试着用p.questioner 做你的事情,我得到undefined method questioner for #&lt;Participant:0x007f85781064e0&gt;
  • 为什么会有两个同名的关联has_many:subjects。
  • 因为参与者可以发布主题(所以他们有has_many :subjects),但他们也可以申请感兴趣的主题,这要归功于表subject_participants。这是 2 个不同的角色
  • 好的,当你参与者.subjects 将调用哪个关联时,我认为你可以为这些关联使用不同的名称。

标签: ruby-on-rails ruby has-many-through model-associations


【解决方案1】:

将您的参与者.rb 更改为

class Participant < ActiveRecord::Base
  .....
  has_many :subject_participants,class_name: "SubjectParticipant",  foreign_key: "interested_id"

end

【讨论】:

  • 好的,这样更好,我没有错误了。但这个要求并不好。我有SELECT "subjects".* FROM "subjects" INNER JOIN "subject_participants" ON "subjects"."id" = "subject_participants"."subject_id" WHERE "subject_participants"."interested_id" = ? [["interested_id", 1]] 我想在subject.questioner_id 上提出请求
  • 您能否更改关联名称您有两个具有相同名称的关联,您可以拥有 has_many :subjects, foreign_key: "questioner_id", class_name: "Participant" 和 has_many :subjects_interested, 通过::subject_participants ,foreign_key:“感兴趣”,class_name:“参与者”#interested
  • 无论何时执行参与者.subjects,它都会调用第二个关联。
【解决方案2】:

你让我找到了解决方案,谢谢你的帮助:

参与者.rb

class Participant < ActiveRecord::Base
  has_many :subject_participants, class_name: "SubjectParticipant", foreign_key: "interested_id"
  has_many :subjects_interested_in, through: :subject_participants, :source => "subject"
  has_many :subjects, foreign_key: "questioner_id"
  has_many :conference_participants
  has_many :conferences, through: :conference_participants
end

主题.rb

class Subject < ActiveRecord::Base
  validates_presence_of  :title, :questioner, :conference, :description

   has_many :subject_participants
   has_many :interested, through: :subject_participants #interested
   belongs_to :questioner, class_name: "Participant"
   belongs_to :conference
end

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多