El Tuesday 31 March 2009 16:18:16 Wes Gamble escribió:
> The issue has to do with the fact that String === String returns false
> and so does Array === Array.
>
> The usage of .class below is in a bogus debug puts and doesn't have
> anything to do with the case statement anyway.
>
> If the final fix was to leave the case statement comparing classes, then
> it still won't work.
It works. Try in irb:
def check_case(value)
case value
when String
puts 'string'
when Array
puts 'array'
else
puts 'else'
end
end
check_case ''
check_case []
>
> Wes
>
> Mukund wrote:
> > The issue has to do with the case statement and the overloaded ===
> > operator. If you want to query the class for an object, don't use
> > the .class operator on the case statement.
> >
> > case joins_for_collection
> > when String
> > [ joins_for_collection ]
> > when Array
> > puts joins_for_collection.inspect
> > joins_for_collection
> > else
> > puts "HERE: #{joins_for_collection.class}"
> > []
> > end + active_scaffold_habtm_joins
> >
> > works without the .class. That is how the final fix was made as
> > well. Just a Ruby annoyance. :)
> >
> >
> > On Mar 26, 1:42 pm, "Sergio Cambra .:: entreCables - Symbol Servicios
> >
> > Informáticos S.L. ::." <[email protected]> wrote:
> >> El Wednesday 25 March 2009 18:56:50 Wes Gamble escribió:
> >>> Wes Gamble wrote:
> >>>> Wes Gamble wrote:
> >>>>> Sergio Cambra .:: entreCables - Symbol Servicios Informáticos S.L.
> >>>>> ::.
> >>>>>
> >>>>> wrote:
> >>>>>> El Wednesday 25 March 2009 15:33:14 Wes Gamble escribió:
> >>>>>>> Sergio Cambra .:: entreCables - Symbol Servicios Informáticos S.L.
> >>>>>>> ::.
> >>>>>>>
> >>>>>>> wrote:
> >>>>>>>> El Sunday 22 March 2009 08:17:18 Wes Gamble escribió:
> >>>>>>>>
> >>>>>>>> I have a fairly complex custom find call that I need to do in some
> >>>>>>>> instances for an AS index view. conditions_for_collection will
> >>>>>>>> only handle conditions, correct? Is there any hook so that I can
> >>>>>>>> provide other find options like :joins or :limit?
> >>>>>>>>
> >>>>>>>> You can define an active_scaffold_joins method, like
> >>>>>>>> active_scaffold_conditions, and you can set the limit in your
> >>>>>>>> controller AS config:
> >>>>>>>> config.list.per_page
> >>>>>>>> http://wiki.github.com/activescaffold/active_scaffold/api-list
> >>>>>>>
> >>>>>>> Sergio,
> >>>>>>>
> >>>>>>> I need to do a join to a HABTM association table - currently I'm
> >>>>>>> doing it as a subquery via conditions_for_collection, as in:
> >>>>>>>
> >>>>>>> "id IN (SELECT medical_facility_id FROM
> >>>>>>> medical_facilities_medical_facility_categories WHERE
> >>>>>>> medical_facility_category_id = #...@medical_facility_category_id})"
> >>>>>>> if @medical_facility_category_id
> >>>>>>>
> >>>>>>> How would I write the active_scaffold_joins (or looks like I should
> >>>>>>> use active_scaffold_habtm_joins) to handle this case?
> >>>>>>
> >>>>>> Sorry, I was wrong.
> >>>>>> conditions_for_collection is for set :conditions option of rails
> >>>>>> find. joins_for_collection is for set :joins option of
> >>>>>> ActiveRecord's find method.
> >>>>>>
> >>>>>> Do you know build the sql with ActiveRecord's find method?
> >>>>>> MedicalFacility.find :all, :joins => :medical_facility_categories,
> >>>>>>
> >>>>>> :conditions => ['medical_facility_categories.id = ?',
> >>>>>>
> >>>>>> @medical_facility_category_id]
> >>>>>>
> >>>>>> So you define these methods:
> >>>>>> def conditions_for_collection
> >>>>>> ['medical_facility_categories.id = ?',
> >>>>>> @medical_facility_category_id] end
> >>>>>>
> >>>>>> def joins_for_collection
> >>>>>> [:medical_facility_categories]
> >>>>>> end
> >>>>>
> >>>>> This is weird. The call to "joins_for_finder" (which calls
> >>>>> joins_for_collection) is failing. I've puts'ed it to death to see
> >>>>> what's happening. Even though the class of my "joins_for_collection"
> >>>>> method is reported as Array, Array is NOT matching on the case
> >>>>> statement, and it is dropping into the else. WTF?
> >>>>>
> >>>>> def joins_for_finder
> >>>>> puts joins_for_collection.class
> >>>>>
> >>>>> case joins_for_collection.class
> >>>>> when String
> >>>>> [ joins_for_collection ]
> >>>>> when Array
> >>>>> puts joins_for_collection.inspect
> >>>>> joins_for_collection
> >>>>> else
> >>>>> puts "HERE: #{joins_for_collection.class}"
> >>>>> []
> >>>>> end + active_scaffold_habtm_joins
> >>>>> end
> >>>>>
> >>>>> Output:
> >>>>> Array
> >>>>> HERE: Array
> >>>>
> >>>> Sure enough, joins_for_collection.class === Array returns false,
> >>>> although joins_for_collection.class == Array returns true.
> >>>>
> >>>> This looks like a bug to me, and now I wonder if joins_for_collection
> >>>> ever worked. Same behavior occurs if a String is returned from
> >>>> joins_for_collection.
> >>>
> >>> This works:
> >>>
> >>> def joins_for_finder
> >>> case joins_for_collection.class.to_s
> >>> when 'String'
> >>> [ joins_for_collection ]
> >>> when 'Array'
> >>> joins_for_collection
> >>> else
> >>> []
> >>> end + active_scaffold_habtm_joins
> >>> end
> >>>
> >>> It makes sense that the "===" doesn't work between classes since it is
> >>> defined thusly:
> >>>
> >>> "Ruby classes are instances of class Class, which defines === to test
> >>> if the argument
> >>> is an instance of the class or one of its superclasses."
> >>>
> >>> Since a class is not an instance of anything, === will never work in
> >>> this case.
> >>>
> >>> I'm on the 2.1 branch - maybe this has been fixed in edge AS?
> >>
> >> Fixed in rails 2.1, 2.2 and edge
> >>
> >> --
> >> Sergio Cambra .:: entreCables 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]
>
>
--
Sergio Cambra .:: entreCables 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
-~----------~----~----~----~------~----~------~--~---