Sorting an array using relationships

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

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

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

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

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

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 ??

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 :slight_smile:

Colin

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

Can’t you just add the status to the order clause?

:order => “status asc, messages.created_at desc”

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]

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.

thanks Tim and i will update u once i completed this module.