Hi all,
My app contains categories, service and discounts.
A category has_many :services
A service belongs_to :category and has_one discount
A discount belongs_to :service
On each category show page I list all of the associated services, but
would like to order them by some of the entries in the discount
table.
i.e. in my categories controller
@category = Category.find(params[:id])
@services=Service.find(:all, :order => "XXXXXXX", :conditions
=>
[''category_id = ?'', @category.id])
where XXXX relates to one of the columns in the discount table. The
column gives the % discount available stored as an integer field.
Any ideas on best practice here,
I thought I could write a method into the Service.rb model file but
although I can call the value easily I can''t order by the entry.
def reduction
return discount.percentage
end
All help appreciated.
Dan C
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> @category = Category.find(params[:id]) > @services=Service.find(:all, :order => "XXXXXXX", :conditions => > [''category_id = ?'', @category.id])@category = Category.find(params[:id]) @category.services.sort! { |x, y| x.reduction <=> y.reduction } --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 22, 3:48 pm, DanC <d.m.coleg...-VlwAjOFxKoqFxr2TtlUqVg@public.gmane.org> wrote:> i.e. in my categories controller > > @category = Category.find(params[:id]) > @services=Service.find(:all, :order => "XXXXXXX", :conditions => > [''category_id = ?'', @category.id]) >If you join the discounts table you can just order by discounts.percentage Fred> where XXXX relates to one of the columns in the discount table. The > column gives the % discount available stored as an integer field. > > Any ideas on best practice here, > > I thought I could write a method into the Service.rb model file but > although I can call the value easily I can''t order by the entry. > > def reduction > return discount.percentage > end > > All help appreciated. > > Dan C--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---