【问题标题】:Ruby on Rails - Wrong foreign key used in has_many through joinRuby on Rails - 通过连接在 has_many 中使用了错误的外键
【发布时间】:2018-07-30 18:36:43
【问题描述】:

Rails 5.2,Postgres 10.4

Rails 新手。我有一个 has_many :through 关系,我正在尝试加入,但是,尽管我的定义,Rails 似乎使用了错误的外键。

用户将通过机会注册来注册许多机会。一个机会可能有许多用户注册。

opportunity.rb

class Opportunity < ApplicationRecord
  has_many :opportunity_enrolment, :class_name => 'OpportunityEnrolment', :foreign_key => "opportunity_id"
  has_many :volunteers, through: :opportunity_enrolment, :foreign_key => "opportunity_id"
end

opportunity_enrolment.rb

class OpportunityEnrolment < ApplicationRecord
  has_many :opportunities, foreign_key: "id"
  has_many :volunteers, foreign_key: "id"
end

users.rb

class Volunteer < ApplicationRecord
  has_many :opportunity_enrolment, :class_name => 'OpportunityEnrolment', foreign_key: "volunteer_id"
  has_many :opportunities, through: :opportunity_enrolment, foreign_key: "opportunity_id"
end

当显示特定用户然后将该用户加入他们的机会时,由于连接创建不正确,我得到机会表中的第一条记录。

<% @volunteer.opportunities.each do |volunteerenrolment| %>
  <table frame="box">
    <tr><td>Name: <%= volunteerenrolment.oppname %></td></tr> --Oppname is in the opportunities table.
    <tr><td>Created: <%= volunteerenrolment.created_at %></td></tr>
  </table>
<% end %>

Rails 创建以下选择语句。

SELECT "opportunities".* FROM "opportunities" INNER JOIN "opportunity_enrolments" ON "opportunities"."id" = "opportunity_enrolments"."id" WHERE "opportunity_enrolments"."volunteer_id" = $1

opportunity_enrolments.id 是 opportunity_enrolments 表上的主键。我需要我的应用加入机会_enrolments.opportunity_id,如下所示:

SELECT "opportunities".* FROM "opportunities" INNER JOIN "opportunity_enrolments" ON "opportunities"."id" = "opportunity_enrolments"."opportunity_id" WHERE "opportunity_enrolments"."volunteer_id" = $1

对于我的一生,我不知道 Rails 从哪里提取了不正确的外键。我曾多次尝试在我的模型中更改它,但 Rails 似乎没有看到那里。如果我使用 .joins 手动编写正确的选择语句,那么一切都很完美,但我想与“配置约定”保持一致

谢谢!

【问题讨论】:

  • 你不觉得应该是has_many :opportunity_enrolments(复数)吗?
  • 三个表的架构是什么样的?
  • @nathan 我认为OpportunityEnrolment 这是连接表,但您在其中设置了错误的关联。请通过以下链接。可能你可以自己解决它。 guides.rubyonrails.org/…
  • 得到了这个答案。我在模型中的单数/复数引用不一致。用户的opportunity_enrolment 关系只是其中之一。 OpportunityEnrolment 模型也存在问题。感谢您的帮助!

标签: ruby-on-rails ruby postgresql


【解决方案1】:

您的OpportunityEnrollment 表是Opportunity 和Volunteer 类之间的粘合剂,应该真正使用belongs_to。另外,您可以使用 rails 默认值,它会为您解释类名和外键。

也不要在名为“users.rb”的文件中定义名为“志愿者”的类

机会.rb

class Opportunity < ApplicationRecord
  # note the plural below as per rails convention
  has_many :opportunity_enrolments
  has_many :volunteers, through: :opportunity_enrolments
end

opportunity_enrolment.rb

class OpportunityEnrolment < ApplicationRecord
  belongs_to :opportunity
  belongs_to :volunteer 
end

志愿者.rb

class Volunteer < ApplicationRecord
  has_many :opportunity_enrolments
  has_many :opportunities, through: :opportunity_enrolments
end

您的连接表应该有 opportunity_id 和 volunteer_id 字段。

【讨论】:

  • 成功了!我想我过度考虑了我的人际关系,然后开始在模型中玩弄单数/复数并陷入意大利面条般的混乱。感谢您的帮助!
猜你喜欢
  • 2012-06-12
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多