Queries with various ruby associations on Rails

Asked

Viewed 153 times

2

I want to do in ruby on Rails, this:

SELECT * FROM isolateds 
  INNER JOIN genes ON geness.isolated_id = isolateds.id
  INNER JOIN stats ON stats.gene_id = genes.id
  INNER JOIN mutations ON stats.mutation_id = mutations.id

When I do:

Isolated.joins(:genes).all, the result is:

SELECT * FROM isolateds 
  INNER JOIN genes ON geness.isolated_id = isolateds.id

But when I do, Isolated.joins(:genes, :stats).all, make a mistake and I can’t even get to the mutations table, what I’m doing wrong?

2 answers

0

The solution to this situation in Rails 3 would be:

Isolated.joins(:genes => :stats => :mutations).all

That would solve the first problem you presented. About the question in your answer, in Rails you are working with Activerecord as a management layer to access the Database, therefore, it would be necessary to see in your models how are the relationships so that one could need if it would be the case to use a left_outer_join or if the object itself after loaded in a variable would no longer bring this information to you.

0

I’ve done enough just doing:

Isolated.joins(:drugs, genes: :mutations ).all

Now my problem and not show me everything of all tables, it is with only two columns, can anyone explain to me why? I would like, even if for a certain isolated, if there were no gene that appears in the same.

Browser other questions tagged

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