pull data from a third table in a single relation

Asked

Viewed 69 times

1

Hi! I have 3 tables. studies, species and origins, where :

estudoanimais belongs_to especie
estudoanimais has_many_procedencias
especie has_many estudoanimais
procedencia belongs_to estudoanimal

The moment I create a provenance, I can associate that with a species, pulled from a select as follows:

f.select :estudoanimais_id, Estudoanimal.all.collect{|ea| [ea.especie.nome,ea.id]}

What I would like is to show the name of this chosen species instead of its id on the provenance screens. On the console, I’m trying to do it this way:

p=Procedencia.first
nome = p.estudoanimal.especie.nome

and he says that he cannot bring that name because it is a null value, but if I put:

p.estudoanimais_id

it brings the id value of the species I chose when creating a source. Where am I missing?

  • Have you tried using the collection_select? Worked on the last Rails project I did

1 answer

0

It got a little complicated, but you’re not bringing changing the ID by the NAME?

f.select :estudoanimais_id, Estudoanimal.all.collect{|ea| [ea.id, ea.especie.nome]}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.