ruby-on-rails – Go get the various fields from the query

Question:

I have this querie:

@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 fields from the model Gene , such as the name field. When I do @everything on the rails console (rails c), this appears to me:

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 the tables being together. if I do @tudo.select("genes.name") , on the rails console it gives 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 I do: @tudo.gene.name gives me an error, how can I get the gene name?

Answer:

I've already figured it out just do: @tudo = @tudo.select("genes.name as genename")

Scroll to Top