In my rails app I have a table Designs which are essentially the posts. Users can like these Designs (using the thumbs_up gem). This table is called Votes. On user's profile pages, I'm showing all of the Designs that they have liked (or voted for). In the user model, I have a method:
def favorites
Design.joins(:votes).where('voter_id = ?', self.id).where('voteable_type = ?', 'Design').where('vote = ?', true)
end
Then in the user's controller to call these designs I have
def show
@designs = @user.favorites
end
This shows all of the designs they have liked, but it's in order of when the Design was created, not when the Vote was created. The Vote table has a created_at column so I know I can sort these Designs based on when they liked them.
I tried this with no luck
def favorites
results = Design.joins(:votes).where('voter_id = ?', self.id).where('voteable_type = ?', 'Design').where('vote = ?', true)
results.sort! {|t1, t2| t2.vote.created_at <=> t1.vote.created_at}
end
How can I sort the Designs based on when that user liked them.
the Vote table has these columns
vote: boolean
voteable_id: integer
voteable_type: string
voter_id: integer
created_at: date
updated_at: date
thanks!