2016-01-25 85 views
1

我想在Rails 4中创建应用程序。Rails 4 - 关联

我有一个配置文件模型和一个组织模型。

Profile.rb:belongs_to:organization 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 %> 

当我尝试这一点,有数字显示没有错误,只是没有显示显示组织名称应该在的地方显示页面。

我想知道是否因为我必须在配置文件模型中为组织标识添加白色标签?

我尝试添加:

organisation_id: [], 

我在配置文件控制器强PARAMS,但它并没有任何区别。

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

当我进入控制台,并尝试:

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... 

任何人都可以看到我做了什么错?

维沙尔的建议:

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... 

维沙尔的下一个建议:

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]] 

TAKING彼得回答以下,

我把我的个人资料控制器显示行动:

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]) 
+0

嘿在你的组织表中没有名称为'profile_id'的列,并且你在'where子句中在这个列上放置了条件所以给你这个问题 –

+0

嗨Vishal,但是为什么我需要把profile_id放在Org中,何时个人资料属于组织? – Mel

+0

是的没有必要把profile_id放在那里,但你可以选择这种方式。其他方面,你可以编写查询,如'Organisation.includes(:profiles).where(:profiles => {:id => 9})' –

回答

0

其实,问题就出现了,因为表单输入字段需要一个复选框。这是一个问题,因为它期望输入数组(每个配置文件只能有一个组织)。当我删除复选框要求时,表单输入元素变成了一个选择列表。然后提交工作以保存组织并正确显示标题。