Lets say I have models
Accounts(:id,:name,:type) has_many :items
Items(:id,:account_id;name) belong_to :account
I want all the items where the account.type = “Cost”
I could do:
income = Item.joins(:account).where(‘account.type’ => “Income”)
Or, I could do:
income = Item.where(:account_id => Account.where(:type =>
While there are several others queries where this is used, the joins
approach takes about 20ms in activerecord. The in approach (the inner
query produces an array of ids) takes about 10ms.
The Items table is expected to be large, the Accounts table will be
Numbers tell me that my array approach is better, but then I have not
seen many use that approach.
Any other suggestions? Comments?