Firebird News

Wednesday, June 02, 2010

Rails tip : joins without associations between models

seems that is possible to create complex joins with no associations between models
first i have tried to use the table name in the :joins/:include parameters area (see the Eager loading of associations in Active Record doc)

conditions[:joins] = "comments"

but doesn't work if there is no association between the model and even if is trough another table/model it doesn't work like i expected
so the solution is to add another join expression in the conditions arguments

conditions[:joins] = "LEFT JOIN comments ON comments.post_id = id"

and then you can filter on comments content or id

conditions[:conditions] ="comments.id=#{some_comment_id}"

then filter the model and use the results

Blog.all conditions

No comments: