I am trying to make an app in Rails 4.
I have a profile model and an organisation model.
Profile.rb: belongs_to :organisation Organisation.rb has_many :profiles.
My profile table has an attribute called :organisation_id.
In my profile show page, I want to show the name of the organisation that the profile belongs to.
I have:
<%= @profile.organisation.try(:title) %>
In my profile form, I ask users to choose their organisation:
<%= f.association :organisation,
as: :check_boxes,
label_method: :title,
value_method: :id,
label: false %>
When I try this, there are no errors showing, just that nothing shows in the show page in the place where the name of the organisation is supposed to be.
I am wondering if thats because I have to white label the organisation id in the profile model?
I tried adding:
organisation_id: [],
to my strong params in the profiles controller, but it didn't make any difference.
When I fill in the profile edit form, I can choose an organisation. I tick the box and expect to see its title in the profiles show page. It's blank.
When I go into the console and try:
o = Organisation.where(profile_id:9)
It gives this error:
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...
Can anyone see what I've done wrong?
Vishal's suggestion:
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's suggestion:
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...
Peter's suggestion adapted (to make profile_id singular rather than plural):
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's next suggestion:
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 PETER'S ANSWER BELOW,
I make my profile controller show action:
def show
@profile = Profile.includes(:industries).find(params[:id])
@organisation = Organisation.find(params[:organisation_id])
@profiles = @organisation.profiles
end
When I save that and try again, I get this error:
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
and you putted condition on this column inwhere
clause so giving you this issue - Vishal JAINOrganisation.includes(:profiles).where(:profiles => {:id => 9})
- Vishal JAINprofile_id
column inorganisations
table, onlyorganization_id
onprofiles
- Andrey Deineko