Rails begin
Rails begin

Reputation: 11

Rails many to one association - Help showing many in one view

These are my models:

class Bedommelse < ActiveRecord::Base belongs_to :virksomhed_primary,
:class_name => 'Virksomhed',
:foreign_key => 'virksomhed_id' belongs_to :virksomheds, 
:foreign_key => "virksomhed_id" 
end

class Bedommelse < ActiveRecord::Base belongs_to :virksomheds, 
:foreign_key => "virksomhed_id" belongs_to :freelances, 
:foreign_key => "freelance_id" 
end

I am trying to display the name of the virksomhed_id not the id itself in the One View (Bedommelse view)

I can show the column virksomhed_id:

   <% @bedommelses.each do |bedommelse| %>
     <p><%= bedommelse.virksomhed_id</p>
   <% end %>

How do I show the name of the virksomhed?

I have tried this but it didn't work.

   <% @bedommelses.each do |bedommelse| %>
     <p><%= bedommelse.virksomhed.navn </p>
   <% end %>

Upvotes: 1

Views: 437

Answers (1)

Rails begin
Rails begin

Reputation: 11

I have found my mistake
I did a fail in the models:
It should be:

belongs_to :virksomhed

not

belongs_to :virksomheds

And the view should be:

<%= @bedommelse.virksomhed.navn %>

Upvotes: 1

Related Questions