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
On 20 September 2011 12:30, Mathew V. [email protected] wrote:
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
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
–
gplus.to/clanlaw
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??
On 20 September 2011 13:53, Mathew V. [email protected] 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
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 ??
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
On 20 September 2011 14:49, Mathew V. [email protected] 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
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]
Can’t you just add the status to the order clause?
:order => “status asc, messages.created_at desc”
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 complete this module.