ruby - Rails: refactoring chained where clauses -
What is the best practice to write something like this? I find it way too cumbersome:
def filtered_components (template) components.joins (: template). Where (templates: {id: template.id}). (Where state: 'work'). Where (['fake_created_at'; time ']', time.New]). Order ('Created DASC') End
There will be a better way, right?
Thank you.
Use the boxes that meet the majority of ActiveRecord . For anything complex, you are going to end up with too many filter chaining. Where combined to complexity def filtered_components (template) components.joins (: template) where little to little where combined. ["Template_id =? AND state =? And fake_created_at In addition, depending on your situation, you may not need template_id
your where
section if you type : template
on : If you are joining the component
, ActiveRecord will translate it:
"template" on "INNER JOIN" templates ".component_id =" components ".id
Comments
Post a Comment