Ordering a collection of items

Items table
id
creator_id
updated_at

Users table
id

Itemlinks table
user_id
item_id

User model

has_many :items_as_creator, :order => created_at
has_many :items_through_itemlinks, :order => created_at

def items
i = items_as_creator + item_through_itemlinks
return i.uniq
end

the question is, how do I ensure that the collection of items
user.items is sorted by created_at? the orders on the has_manys don’t
work: consider

items_as_creator { {item created 2 seconds ago }, {item created 3
seconds ago} }
items_through_itemlinks { {item created 1 second ago}, {item created 2
seconds ago} }

then items is like { {item created 2 seconds ago }, {item created 3
seconds ago} {item created 1 second ago} }

i want items to be in the order { {item created 1 seconds ago }, {item
created 2 seconds ago} {item created 3 second ago} }

please help! :slight_smile:

solution:

i.uniq!
i = i.sort_by { |idea| idea.created_at }
i.reverse!

item_id

the question is, how do I ensure that the collection of items
user.items is sorted by created_at? the orders on the has_manys don’t
work: consider

maybe…

User model

has_many :items_as_creator, :order => ‘items.created_at’

?

nope, each of the items sets by created_at doesn’t work for the reasons
explained above.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs