首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4-关联

Rails 4-关联
EN

Stack Overflow用户
提问于 2016-01-25 14:53:57
回答 1查看 52关注 0票数 1

我正在尝试用Rails4做一个应用程序。

我有一个个人资料模型和一个组织模型。

Profile.rb: belongs_to :organisation Organisation.rb has_many :profiles。

我的profile表有一个名为:organisation_id的属性。

在我的个人资料显示页面中,我希望显示该个人资料所属的组织的名称。

我有:

代码语言:javascript
复制
                <%= @profile.organisation.try(:title) %>

在我的个人资料表单中,我要求用户选择他们的组织:

代码语言:javascript
复制
<%= f.association :organisation,
                     as: :check_boxes,
                     label_method: :title,
                     value_method: :id,
                     label: false  %>

当我尝试这样做时,没有错误显示,只是在显示页面中组织名称应该出现的地方没有显示任何内容。

我想知道这是不是因为我必须在配置文件模型中给组织id加上白标签?

我尝试添加:

代码语言:javascript
复制
organisation_id: [],

添加到配置文件控制器中的强参数,但这并没有什么不同。

当我填写个人资料编辑表单时,我可以选择一个组织。我勾选了这个框,并期望在个人资料显示页面中看到它的标题。它是空白的。

当我进入控制台并尝试:

代码语言:javascript
复制
o = Organisation.where(profile_id:9)

它会给出这个错误:

代码语言:javascript
复制
  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的建议:

代码语言:javascript
复制
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...

Peter的建议:

代码语言:javascript
复制
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成为单数而不是复数):

代码语言:javascript
复制
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的下一个建议是:

代码语言:javascript
复制
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]]

下面是PETER的答案,

我让我的配置文件控制器显示动作:

代码语言:javascript
复制
  def show
    @profile = Profile.includes(:industries).find(params[:id])
    @organisation = Organisation.find(params[:organisation_id])
    @profiles = @organisation.profiles
  end

当我保存它并重试时,我得到了这个错误:

代码语言:javascript
复制
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])
EN

回答 1

Stack Overflow用户

发布于 2016-01-25 17:36:33

实际上,出现这个问题是因为表单输入字段要求一个复选框。这是一个问题,因为它需要一组输入(当每个配置文件只能有一个组织时)。当我删除复选框要求时,表单输入元素变成一个选择列表。然后,提交即可保存组织,标题也会正确显示。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34986682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档