我想在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])
嘿在你的組織表中沒有名稱爲'profile_id'的列,並且你在'where子句中在這個列上放置了條件所以給你這個問題 –
嗨Vishal,但是爲什麼我需要把profile_id放在Org中,何時個人資料屬於組織? – Mel
是的沒有必要把profile_id放在那裏,但你可以選擇這種方式。其他方面,你可以編寫查詢,如'Organisation.includes(:profiles).where(:profiles => {:id => 9})' –