Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - Get parent attribute from child model

In my Ruby app I have two classes Event and Company, such that Event...

belongs_to :company 

Then in event.jst.eco I have the following code...

 <span><%= @event.get'company_id'=></span>

However instead of returning the company_id I want to return the company name which is an attribute of the parent class Company.

I would have thought it was something like...

<%= @event.company.get('name') %> 

but that just gives me...

ExecJS::ProgramError in Site#index

How can I achieve this?

like image 283
billett Avatar asked May 03 '26 23:05

billett


1 Answers

You should write as :-

<span><%= @event.company.name =></span>

As per the belongs_to association inside the Event model, it must have a method called #company, which will give you the associated Company object.

like image 102
Arup Rakshit Avatar answered May 05 '26 13:05

Arup Rakshit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!