views:

55

answers:

1

I must connect from my Rails app to a remote database on a Java application.

I have a query like this:

find_by_sql("select c_templateid, c_templateinfoid, c_startdate, c_enddate, c_active,
campaign, shortcode, prefix, c_descriptivename, c_description, c_templatename 
from (active_services aser join activity a on aser.c_primaryprefixid =
a.c_primaryprefixid ) join matrix_templateinfo using(c_templateinfoid) 
where campaign is not null)")

I need to refactor it to the AR#find() method, because I want later add complex :conditions on it. I dont want convert them into string to append then later to the find_by_sql method.

find(:all, 
  :select => "c_templateid, c_templateinfoid, c_startdate, c_enddate, c_active,
  campaign, shortcode, prefix, c_descriptivename, c_description, c_templatename",
# :joins => WHAT I SHOULD DO HERE ?
  :conditions => "campaign is not null"
)
+1  A: 

You can also specify complex joins in :joins

:joins => "matrix_templateinfo ON <conditions go here> AND campaing IS NOT NULL"

but you should really start padronizing your field names if you're using rails :]

Angelus
I have refactored it http://gist.github.com/487539 , but I don't know are this queries equal ?
astropanic