ruby on rails - Arrange items in an array by the time they were added to that array? -


basically have collection model , post model, collection has many posts , post belongs many collections. i'll push posts @collection.posts array using <<, replicate post being added collection. there way order posts in @collection.posts time pushed array? if yes, how?

all relevant models:

user.rb

class user < activerecord::base    has_many :posts, dependent: :destroy   has_many :collections, dependent: :destroy end 

post.rb

class post < activerecord::base    belongs_to :user   has_many :collectables   has_many :collections, through: :collectables end 

collection.rb

class collection < activerecord::base   belongs_to :user   has_many :collectables   has_many :posts, through: :collectables end 

collectable.rb

class collectable < activerecord::base   belongs_to :post   belongs_to :collection end 

i guess adding order scope definition of association work:

# in collection.rb has_many :posts,          -> { order('collectables.created_at desc') },          through: :collectables 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -