Wednesday, 15 August 2012

ruby on rails - Create array from ActiveRecords Association CollectionProxy -



ruby on rails - Create array from ActiveRecords Association CollectionProxy -

i have grouping model has_many topics. topics model has_many posts. want create array of topics grouping sorted post attribute :published_on.

on grouping show page have @group.topics.collect {|x| x.posts } returns activerecord::associations::collectionproxy array each element containing array of post objects.

[ [[topic1 post],[topic1 post],[topic1 post]], [[topic2 post],[topic2 post],[topic2 post]], [[topic3 post],[topic3 post],[topic3 post]], ]

how create single array of posts sorted :published_on ?

i think

group.topics.includes(:posts).order("posts.published_on").map(&:posts).flatten

would enough.

ruby-on-rails ruby arrays activerecord ruby-on-rails-4

No comments:

Post a Comment