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! :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
solution:
i.uniq!
i = i.sort_by { |idea| idea.created_at }
i.reverse!
On Nov 3, 2:35 am, "postscript07"
<postscrip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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! :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
> 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: considermaybe... User model ------------------ has_many :items_as_creator, :order => ''items.created_at'' ?> > 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! :) > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
nope, each of the items sets by created_at doesn''t work for the reasons explained above. On Nov 3, 3:09 am, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > 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: considermaybe... > > User model > ------------------ > has_many :items_as_creator, :order => ''items.created_at'' > > ? > > > > > 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! :)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---