gpt4 book ai didi

ruby-on-rails - 尝试在两个模型之间进行 Rails 数据库关联/查询会引发错误

转载 作者:行者123 更新时间:2023-11-29 13:41:57 26 4
gpt4 key购买 nike

我正在尝试在两个模型(提交和代理事件)之间创建关联,每个代理都可以在不更改实际提交记录本身的情况下记录针对提交的事件。

这些是我的 rb 文件,我相信这些文件设置正确:

class Submission < ApplicationRecord
belongs_to :user, :optional => true

belongs_to :location, :optional => true

has_many :agent_activities

end


class AgentActivity < ApplicationRecord

belongs_to :submission, :optional => true #has submission_id foreign key in table
belongs_to :agent, :optional => true #has agant_id foreign key in
table


end

但是,在 Controller 中显示此关联时,我迷路了。目前我的代码如下所示:

@submissions = Submission.where(:Desired_Location => current_agent.Company_Business_Location).pluck(:AgentActivity)

但是,当我这样做时,我得到了这个错误

PG::UndefinedColumn: ERROR: column "AgentActivity" does not exist 
LINE 1: SELECT "AgentActivity" FROM "submissions" WHERE
"submissions... ^ : SELECT "AgentActivity" FROM "submissions" WHERE
"submissions"."Desired_Location" = $1

此外,当我运行时

Submission.where(:Desired_Location => 
current_agent.Company_Business_Location).map(&:AgentActivity)

我收到这个错误:

undefined method `AgentActivity' for #<Submission:0x007fc78601e168> 
Did you mean? agent_activities

当我运行时:

AgentActivity.joins(:submission).where('submission.Desired_Location' 
=> current_agent.Company_Business_Location)

我收到这个错误:

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table 
"submission"
LINE 1: ..."."id" = "agent_activities"."submission_id" WHERE
"submissio...
^
: SELECT "agent_activities".* FROM "agent_activities" INNER JOIN
"submissions" ON "submissions"."id" =
"agent_activities"."submission_id" WHERE
"submission"."Desired_Location" = $1

最佳答案

你在类 提交 中有错字

应该是has_many :agent_activities

关于ruby-on-rails - 尝试在两个模型之间进行 Rails 数据库关联/查询会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com