【发布时间】:2016-01-25 06:53:56
【问题描述】:
我正在尝试在 Rails 4 中制作应用程序。
我有一个档案模型和一个组织模型。
Profile.rb: belongs_to :organisation Organisation.rb has_many :profiles.
我的个人资料表有一个名为 :organisation_id 的属性。
在我的个人资料显示页面中,我想显示个人资料所属组织的名称。
我有:
<%= @profile.organisation.try(:title) %>
在我的个人资料表单中,我要求用户选择他们的组织:
<%= f.association :organisation,
as: :check_boxes,
label_method: :title,
value_method: :id,
label: false %>
当我尝试这个时,没有显示任何错误,只是在显示页面中应该显示组织名称的位置没有显示任何内容。
我想知道这是不是因为我必须在配置文件模型中为组织 ID 贴上白标?
我尝试添加:
organisation_id: [],
我在配置文件控制器中的强大参数,但它没有任何区别。
当我填写个人资料编辑表时,我可以选择一个组织。我勾选该框并希望在配置文件显示页面中看到它的标题。一片空白。
当我进入控制台并尝试时:
o = Organisation.where(profile_id:9)
它给出了这个错误:
Organisation Load (6.1ms) SELECT "organisations".* FROM "organisations" WHERE "organisations"."profile_id" = 9
PG::UndefinedColumn: ERROR: column organisations.profile_id does not exist
LINE 1: ...LECT "organisations".* FROM "organisations" WHERE "organisat...
^
: SELECT "organisations".* FROM "organisations" WHERE "organisations"."profile_id" = 9
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column organisations.profile_id does not exist
LINE 1: ...LECT "organisations".* FROM "organisations" WHERE "organisat...
谁能看到我做错了什么?
Vishal 的建议:
Organisation.includes(:profiles).where(:profile => {:id => 9})
SyntaxError: (irb):55: syntax error, unexpected tCONSTANT, expecting end-of-input
...s.map(&:table_name)Organisation.includes(:profiles).where(:p...
彼得的建议:
Organisation.where(profile_ids: [9])
Organisation Load (35.5ms)
SELECT "organisations".* FROM "organisations" WHERE "organisations"."profile_ids" = 9
PG::UndefinedColumn: ERROR: column organisations.profile_ids does not exist
LINE 1: ...LECT "organisations".* FROM "organisations" WHERE "organisat...
^
: SELECT "organisations".* FROM "organisations" WHERE "organisations"."profile_ids" = 9
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column organisations.profile_ids does not exist
LINE 1: ...LECT "organisations".* FROM "organisations" WHERE "organisat...
改编彼得的建议(使 profile_id 单数而不是复数):
Organisation.where(profile_id: [9])
Organisation Load (36.9ms) SELECT "organisations".* FROM "organisations" WHERE "organisations"."profile_id" = 9
PG::UndefinedColumn: ERROR: column organisations.profile_id does not exist
LINE 1: ...LECT "organisations".* FROM "organisations" WHERE "organisat...
^
: SELECT "organisations".* FROM "organisations" WHERE "organisations"."profile_id" = 9
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column organisations.profile_id does not exist
LINE 1: ...LECT "organisations".* FROM "organisations" WHERE "organisat...
Vishal 的下一个建议:
Organisation.includes(:profiles).where(:profiles => {:id => 9})
SQL (18.9ms) SELECT "organisations"."id" AS t0_r0, "organisations"."title" AS t0_r1, "organisations"."logo" AS t0_r2, "organisations"."banner" AS t0_r3, "organisations"."org_type" AS t0_r4, "organisations"."email_format" AS t0_r5, "organisations"."website" AS t0_r6, "organisations"."formal_name" AS t0_r7, "organisations"."company_number" AS t0_r8, "organisations"."jurisdiction_of_incorporation" AS t0_r9, "organisations"."onboarded" AS t0_r10, "organisations"."cf_docs" AS t0_r11, "organisations"."user_id" AS t0_r12, "organisations"."created_at" AS t0_r13, "organisations"."updated_at" AS t0_r14, "organisations"."abn" AS t0_r15, "profiles"."id" AS t1_r0, "profiles"."user_id" AS t1_r1, "profiles"."title" AS t1_r2, "profiles"."hero" AS t1_r3, "profiles"."overview" AS t1_r4, "profiles"."occupation" AS t1_r5, "profiles"."external_profile" AS t1_r6, "profiles"."working_languages" AS t1_r7, "profiles"."created_at" AS t1_r8, "profiles"."updated_at" AS t1_r9, "profiles"."organisation_id" AS t1_r10 FROM "organisations" LEFT OUTER JOIN "profiles" ON "profiles"."organisation_id" = "organisations"."id" WHERE "profiles"."id" = $1 [["id", 9]]
下面是彼得的答案,
我让我的配置文件控制器显示操作:
def show
@profile = Profile.includes(:industries).find(params[:id])
@organisation = Organisation.find(params[:organisation_id])
@profiles = @organisation.profiles
end
当我保存并重试时,我收到此错误:
ActiveRecord::RecordNotFound in ProfilesController#show
Couldn't find Organisation with 'id'=
Extracted source (around line #17): - it points to this line:
@organisation = Organisation.find(params[:organisation_id])
【问题讨论】:
-
嘿,您的组织表中没有名称为
profile_id的列,并且您在where子句中为该列设置了条件,所以给您这个问题 -
嗨,Vishal,但是当配置文件属于 Org 时,为什么我需要将 profile_id 放入 Org 中?
-
是的,无需将 profile_id 放在那里,但您可以选择这种方式。否则,您可以编写类似
Organisation.includes(:profiles).where(:profiles => {:id => 9})的查询 -
一个配置文件有一个organization_id,一个组织根据你的关联有一个profile_ids。表单中的值应该是 organization_id 而不是 id。在您的示例查询中,它将是:Organization.where(profile_ids: [9]),但您实际上想要的是:Profile.where(organization_id: params[:organization_id])
-
@VishalJAIN 否,OP 不需要
organisations表中的profile_id列,只需要organization_id上的profiles
标签: ruby-on-rails associations model-associations