Hi, 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/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Why are you storing the comment date in a separate table? The code you had is pretty close. Assuming you have a "posted_date" on the comments_date model, and the commends model has_one(:comments_date): @comments.sort do |c| c.comments_date.posted_date end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/YEYVj4ohUhwJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 September 2011 12:30, Mathew Vivek <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > 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 > endIt 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- gplus.to/clanlaw -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
this is the code @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?? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 September 2011 13:53, Mathew Vivek <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> this is the code > > > @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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks all, 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 ?? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 September 2011 14:49, Mathew Vivek <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, 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 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Can''t you just add the status to the order clause? :order => "status asc, messages.created_at desc" -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/xIJmfUbamUsJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thanks tim for your response i have tried that one. but i have to sort in this order status = [1,4,5,6,2,3,8,9,7] -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I believe in MySQL and PostgreSQL you can do something like this: order by status in (1,4,5,6,2,3,8,9,7) asc Which would translate to something like this in Rails: :order => "status in (1,4,5,6,2,3,8,9,7) asc, messages.created_at desc" Not sure if that will work verbatim though. You might have to play around with it a little bit. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/z8p1nOsd4nMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thanks Tim and i will update u once i completed this module. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.