3
I have this darling:
@tudo = Isolated.joins("LEFT JOIN resists ON resists.isolated_id = isolateds.id").joins("LEFT JOIN genes ON genes.isolated_id = isolateds.id LEFT JOIN stats ON stats.gene_id = genes.id LEFT JOIN mutations ON mutations.id = stats.mutation_id").all
I want to get model fields Gene
, as for example the field name
.
When I do @everything on the Rails console (Rails c), I see this:
Isolated Load (4.4ms) SELECT `isolateds`.* FROM `isolateds` LEFT JOIN resists ON resists.isolated_id = isolateds.id LEFT JOIN genes ON genes.isolated_id = isolateds.id LEFT JOIN stats ON stats.gene_id = genes.id LEFT JOIN mutations ON mutations.id = stats.mutation_id
=> #<ActiveRecord::Relation [#<Isolated id: 1, name: "xpto", disease: "sklhafl", n_samples: 1, origin_id: 1, organism_id: 3, created_at: "2015-03-23 16:21:20", updated_at: "2015-03-23 16:21:20">, #<Isolated id: 2, name: "khjlsdkf", disease: "lkajsçdl", n_samples: 123, origin_id: 1, organism_id: 1, created_at: "2015-03-26 18:57:02", updated_at: "2015-03-26 18:57:02">,...
That is, only data from Isolated, even though the tables are together.
if made @tudo.select("genes.name")
, on the console of the Rails give me this:
#<ActiveRecord::Relation [#<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, #<Isolated id: nil, name: nil>, ...]>
and when in the html.erb file:
@tudo.gene.name
gives me error, how can I get the name of the gene?
I’ve found it enough to do: @tudo = @tudo.select("genes.name as genename")
– Catarina Silva
Add your comment to a question, and accept it as an answer, as it may help other users with the same problem, or similar problems.
– Vinícius Gobbo A. de Oliveira