JimboTwice JimboTwice - 11 months ago 26
Ruby Question

Eager loading database table using ActiveRecord.

I have a Ruby 2.2.2 app that uses ActiveRecord as the ORM (similar to Rails). I have two tables, "users" and "accounts", where "accounts" belongs_to :user. For a given user, I simply want to eager load the contents of the "accounts" table into a Ruby object (named user_accounts) so I can perform operations like:


...without the find_by_product_name method performing a SQL query. I simply want to preload all entries from the "accounts" table (that belong to a given user) into a Ruby object so I can minimize the number of SQL queries performed.

No matter how much documentation I read, I cannot figure out how to do this properly. Is this possible? If so, how?


If you don't want the ORM to re-query the database, then I think you are better of using the select method added by the Enumerable mixin. Because if you try to use the find_by_* methods, I think it will always send another query.

Here is an example of how it could be achieved.

# The accounts association will be loaded and cached the first time
@user.accounts.select { |account| account.name == "nameofproduct" }

# The next time, it is already loaded and will select from the previously loaded records
@user.accounts.select { |account| account.name == "nameofanotherproduct" }