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
thanks in advance
--
Posted via http://www.ruby-forum.com/.
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.
Colin
>
> thanks in advance
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
@posts = Post.all(:include=>[:messages],:conditions => ["status in (?)
", [1,2,3,4,5,6,8,9]],:order => "messages.created_at DESC")
plz check whether it is correct syntax??
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.
Colin
--
gplus.to/clanlaw
scope :get_by_publish_on, lambda{ |status|
joins(:postmessages).where('status in (?)', status).order("publish_on
desc").group('posts.id') }
thanks colin for your idea, you are insisting like this ??
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 ??
I am not insisting on anything, just suggesting :)
Colin
--
gplus.to/clanlaw
I am having a New Requirement.
I want to sort a array of elements in which
@posts = Post.all(:include=>[:messages],:conditions => ["status in (?)
", [1,2,3,4,5,6,8,9]],:order => "messages.created_at DESC")
posts table has a column named status as u see above.
i want to sort this array by status parameters..
status = [1,4,5,6,2,3,8,9,7] and messages.created_at DESC
i have tried that one.
but i have to sort in this order
status = [1,4,5,6,2,3,8,9,7]