Just saw your post then Sergio as I am about to post the below :)
I'd basically done what you've suggested with more to handle :order as
well as :conditions.
module TransactionHelper
def options_for_association_conditions(association)
if association.name == :category
{:conditions => ['active = 1 OR active is null'], :order => 'id
asc'}
else
super
end
end
end
module ActiveScaffold
module Helpers
module Associations
# Provides a way to honor the :conditions on an association
while searching the association's klass
def association_options_find(association, conditions = nil)
find_conditions = conditions
find_order = nil
if (conditions && conditions.length > 1)
find_conditions = conditions[:conditions] || nil
find_order = conditions[:order] || nil
end
association.klass.find(:all, :conditions =>
controller.send(:merge_conditions, find_conditions,
association.options[:conditions]), :order => find_order)
end
def association_options_count(association, conditions = nil)
find_conditions = conditions
if (conditions && conditions.length > 1)
find_conditions = conditions[:conditions] || nil
end
association.klass.count(:all, :conditions =>
controller.send(:merge_conditions, find_conditions,
association.options[:conditions]))
end
# returns options for the given association as a collection of
[id, label] pairs intended for the +options_for_select+ helper.
def options_for_association(association)
available_records = association_options_find(association,
options_for_association_conditions(association))
available_records ||= []
#available_records.sort{|a,b| a.to_label <=>
b.to_label}.collect { |model| [ model.to_label, model.id ] }
available_records.collect { |model| [ model.to_label,
model.id ] }
end
def options_for_association_count(association)
association_options_count(association,
options_for_association_conditions(association))
end
# A useful override for customizing the records present in an
association dropdown.
# Should work in both the subform and form_ui=>:select modes.
# Check association.name to specialize the conditions per-
column.
def options_for_association_conditions(association)
return nil if association.options[:through]
case association.macro
when :has_one, :has_many
# Find only orphaned objects
"#{association.primary_key_name} IS NULL"
when :belongs_to, :has_and_belongs_to_many
# Find all
nil
end
end
end
end
end
On Sep 3, 8:03 pm, "Sergio Cambra .:: entreCables - Symbol Servicios
Informáticos S.L. ::." <[EMAIL PROTECTED]> wrote:
> You can override options_for_association like you do with
> options_for_association_conditions
>
> def options_for_association(association)
> available_records = association_options_find(association,
> options_for_association_conditions(association))
> available_records ||= []
> available_records.sort{|a,b| a.to_label <=> b.to_label}.collect { |model| [
> model.to_label, model.id ] }
> end
>
> You can use something like this:
> def options_for_association(association)
> if association.name == :category
> available_records = association_options_find(association,
> options_for_association_conditions(association))
> available_records ||= []
> available_records.sort{|a,b| a.to_label <=> b.to_label}.collect { |model|
> [ model.to_label, model.id ] }
> else
> super
> end
> end
>
> You can change the line which does the sorting
>
> El Tuesday 02 September 2008 08:37:11 greghauptmann escribió:
>
>
>
> > but I'm filtering on the records for one of the associations, and
> > using the helper method "options_for_association_conditions"? So it
> > seems that the output of this method is for the ":conditions"..... So
> > my "app/helpers/transaction_helper.rb" file looks like the below.
> > Again my aim again is to get sorting on the drop down list for this
> > "category" association...
>
> > ==========================
> > module TransactionHelper
> > def category_column(record)
> > Category.find(:all, :conditions => 'active = true')
> > end
>
> > def options_for_association_conditions(association)
> > if association.name == :category
> > ['active = 1 OR active is null']
> > else
> > super
> > end
> > end
>
> > end
> > ==========================
>
> > On Sep 2, 2:14 pm, Sri Sri <[EMAIL PROTECTED]> wrote:
> > > Try :order.
> > > ....find(:all, :conditions => "", :order => "")
>
> > > greghauptmann wrote:
> > > > thanks for this thread re how to filter down the number of records in
> > > > a SELECT. Is there a way however to be able to modify the SORT
> > > > ORDER of the pull down list? I tried putting a "order by" in the
> > > > options_for_association_conditions however the SQL generated doesn't
> > > > work, as it is creating a "where( xxx )" in brackets.
>
> > > > Any ideas how to get sorting on a field drop down list for an
> > > > association working? (i.e. without having to do a full field
> > > > override)
>
> > > > tks
>
> --
> Sergio Cambra .:: entreCables - Symbol Servicios Informáticos S.L. ::.
> Nicolás Guillén 6, locales 2 y 3. 50.018 Zaragoza
> T) 902 021 404 F) 976 52 98 07 E) [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---