I have in my model

class Card < ActiveRecord::Base
  has_many  :idioms,    dependent: :destroy
end

class Idiom < ActiveRecord::Base
  belongs_to :card
end

In my schema, Idiom has an integer column kind. Given a certain card, I
would like to have all associated idioms, but sorted in descending order
according to the 'kind' column.

I could do a

  @card.idioms.sort { .... }

but would prefer doing the sorting by the time the data is retrieved
from the database. I googled two suggestions:

(1) @card.idioms(:order => 'kind DESC')

This doesn't seem to have any effect.

(2) @card.idioms.all(:order => 'kind DESC')

This gives the error "wrong number of arguments (1 for 0)".

I think I could do a

Idiom.where(....)

and put an order restriction there, but I feel that Rails must have a
way to specify sorting when following associations, and maybe I just
made some silly mistake. Any ideas?

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/24d5871e8a39c5a3963122c9dffeeefc%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to