In Rails, I have a model named Director which references a table with a field named also ‘director’.
When I do queries that use joins (either with the relationship in the model or using the .joins method) then try to print the director field, I get the relationship instead of the field, so when doing
<% @dvds.each do |dvd| %>
<%= "#{dvd.director}" %>
I get something like:
#<Director id: 93, director: "Brad Bird">
When I should get just “Brad Bird”.
Is there a way to disambiguate this and get only the field name without having to change my Model names?
You can do two things:
Use the field like:
Or add a to_s to the model like:
By the way, it’s a bit weird to have a
directorfield on aDirectormodel