I have a subscription application with Users, Orders and DeliverySchedules (and other models that make a has_many :through necessary instead of a has_many).
class User < ActiveRecord::Base
has_many :orders
has_many :deliveryschedules, :through => :orders
class Deliveryschedule < ActiveRecord::Base
has_many :orders
has_many :users, :through => :orders
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :deliveryschedule
My question is, why does
@allusers.each do |t|
User.find_by_id(t.id).deliveryschedules.last.delivery_date
end
return something different from
@allusers.each do |t|
t.deliveryschedules.last.delivery_date
end
My app is working, but I feel like I’m missing something fundamental here I can’t track down in the docs.
This is normal database behavior. The order of records that are returned from the database is undetermined, unless you specifically use
ORDER BY. So it is possible that two different queries will return the rows in different order.