Use maps.

They’re a good tool for complex queries.

Simple queries can use the basics of ActiveRecord. The model matches the database table, and operations on the model can mimic the queries on the table. For many operations, this is all you need.

In more complicated scenarios, you will need to join data from two tables and return them in a result set. By mapping the results from a complex query into a result set, you can more easily access and manipulate the data in the rest of your application.

Don’t assemble these maps using a for loop. Instead, return the query results straight into the map, so it lands in the result set in one action.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: