I have a visit model as following
class Visit < ActiveRecord::Base
belongs_to :engineer
has_many :machines
end
What i’m trying to do is find the number of machines associated with a particular engineer by using following code
count = engineer.visits.map(&:machines).flatten.size
is there any better and faster way to do this? I don’t want to create a direct association between engineer and machines.
You can for your query by using the model keywords like join, includes. while selecting one record form a model.
For your reference http://archives.ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions, http://railscasts.com/episodes/202-active-record-queries-in-rails-3