Go get the various fields to the query

3

I have this query:

@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 fetch fields from the Gene model, such as the name field. When I do the @ everything in 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 Isolated data, even the tables being put together. if you do @tudo.select("genes.name") , in the rails console 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 I do: @tudo.gene.name gives me error, how can I go get the gene name?

    
asked by anonymous 18.04.2015 / 01:10

2 answers

1

I have already found out just do: @ everything = @ everything.select ("genes.name as genename")

    
18.04.2015 / 12:43
0

Catherine, in the way it was defined, when we call @ everything, the server responds with a list. In case, when calling @ todo.gene it is normal that you miss an error.

To get a list of gene names, you could use the method map .

@tudo.map(&:name)

What the map method does is to execute the command that follows the colon (":") for each item in its list of genes and return the result in the form of an array.

I hope I have helped =)

    
27.04.2015 / 20:01