ruby on rails 3 - ActiveRecord sort children within parent -
is there way pre-sort children of parent through activerecord (rails 3.2.13)?
so if have setup this
class parent < activerecord::base has_many :children [...] class children < activerecord::base belongs_to :parent
something like this:
p = parent.where(:name => 'diana').includes(:children, :order => 'd_o_b desc')
that way when call p.children
getting array of objects ordered birth, , not database id.
or need sort array afterward?
in parent
model, change has_many
to:
has_many :children, :order => 'd_o_b desc'
then anytime access children
association parent record (e.g., @parent.children
), they'll in descending order of date of birth.
Comments
Post a Comment