mercredi 15 avril 2015

Rails active record query to string

When I have the AR query like this :



MyModel.joins(:related_model).where('related_model.some_column = ?', 'somevalue')


If I used to_sql on the above code it would generate something like :



"SELECT * FROM MY_MODEL INNER JOIN RELATED_MODEL ON RELATED_MODEL.SOME_ID = MY_MODEL.SOME_ID WHERE RELATED_MODEL.SOME_COLUMN = 'SOMEVALUE'"


I'm creating a slightly, or way more complex query, and I hate to duplicate code. Is there are way that I can get this part with some command to alter the generated SQL below?



INNER JOIN RELATED_MODEL ON RELATED_MODEL.SOME_ID = MY_MODEL.SOME_ID WHERE RELATED_MODEL.SOME_COLUMN = 'SOMEVALUE'"

Aucun commentaire:

Enregistrer un commentaire