Wes Gamble wrote:
> AS 2.1
> Rails 2.1
>
> Two models: A and B.  A HABTM B.
>
> I have a specialized join for an index view and there is a "name" column 
> in both A and B.  I want to show A.name, but in a certain case, I am 
> getting B.name.
>
> How can I force the query to be generated so that A.name is returned.
>
> I tried creating a method on A called "facility_name", have it just 
> return the value of "name" and then putting "facility_name" that into 
> the set of list columns, but that doesn't seem to work.
>
> So, in general, how do we disambiguate select columns in AS generated 
> queries.
>   
Here's what I ended up doing:

1) Since I added the custom_finder_options feature, I simply provided a 
:select option to be used in the find call, like so:

opts[:select] = ['medical_facilities.name, address, phone, class_of_trade']

Unfortunately, this select clause must now mirror the 
config.list.columns array exactly since it is effectively overriding the 
generation of the SELECT clause for the query that is normally provided 
by config.list.columns.

2) However, because the arbitrary finder options were also being passed 
to the COUNT query in Finder#find_page, I also had to hack the count 
query line to ignore the select option, like so:

count = klass.count(finder_options.reject{|k,v| [:order, 
:select].include? k})   #Added :select to the list of options to ignore.

This gives me queries like so:

Count query:

SELECT count(*) AS count_all FROM `medical_facilities` INNER JOIN 
`medical_facilities_medical_facility_categories` ON 
`medical_facilities_medical_facility_categories`.medical_facility_id = 
`medical_facilities`.id INNER JOIN `medical_facility_categories` ON 
`medical_facility_categories`.id = 
`medical_facilities_medical_facility_categories`.medical_facility_category_id 
WHERE (medical_facility_category_id = '1')

List select query:

SELECT medical_facilities.name, address, phone, class_of_trade FROM 
`medical_facilities` INNER JOIN 
`medical_facilities_medical_facility_categories` ON 
`medical_facilities_medical_facility_categories`.medical_facility_id = 
`medical_facilities`.id INNER JOIN `medical_facility_categories` ON 
`medical_facility_categories`.id = 
`medical_facilities_medical_facility_categories`.medical_facility_category_id 
WHERE (medical_facility_category_id = '1' )

This works, but I don't like it.

The other option would have been to change the name of one of the "name" 
columns in one of the tables so that there could be no name collision in 
the result set generation.  Which also sucks.

Wes

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to