【问题标题】:How to post to a join table in a rails controller如何发布到 Rails 控制器中的连接表
【发布时间】:2023-04-09 05:20:02
【问题描述】:

我正在尝试添加与以下模型的多对多关系:

class Account < ApplicationRecord
  belongs_to :user
  has_many :issues, through: :interests
end

class Issue < ApplicationRecord
  has_many :accounts, through: :interests
end

class Interest < ApplicationRecord
  belongs_to :account
  belongs_to :issue
end

当我尝试在我的accounts_controller 中调用以下方法时

def add_issue
  issue = Issue.find(params[:id])
  @account = current_user.account
  @account.interests.create(issue: issue)
end

读取undefined method 'interests' for #&lt;Account:0x007fe50e23b658&gt; 时出现错误。发布到联接表的正确方法是什么?

【问题讨论】:

    标签: ruby-on-rails postgresql database-relations


    【解决方案1】:

    #<0x007fe50e23b658>

    &lt;&lt;

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多