I want to sort a array objects which depends upon relationships between
two tables.
table 1 : comments
table 2 : comments_date
Once a comment is added, the date which it is created will be saved in
comments date table.. now i want to sort the
comments array with respect to date it is created..
the code looks like this..
@comments.sort_by do |a|
a.comments_date
end
It would be more efficient to sort it when you get it from the db. So
in the find use something like :include => :comments_dates and then
have an order clause specifying comments_dates.date (or whatever the
column is called in the comments_dates table.
I have to agree with Tim that it seems odd to have a separate table
for the date rather than just a column in the comments table.
That does not seem to have anything to do with your previous post.
Did you mean column rather than table in your previous post? If so
then it would be better to explain rather than just ignoring this.
It is easier for you to check by trying it and see if it runs and if
it passes your automated tests.
I suggest adding a named scope to your model for this, then the
complexity of the code will be in the model not the controller.
Please quote from the previous message and insert your comments at
appropriate points. It makes it easier to follow the thread. Thanks
scope :get_by_publish_on, lambda{ |status|
joins(:postmessages).where('status in (?)', status).order("publish_on
desc").group('posts.id') }
Why have you used joins rather than include? You should not need the
group spec. Your names seem to change at every post, it makes it
difficult to work out what you are trying to do.
thanks colin for your idea, you are insisting like this ??