Yeah this looks like a nice solution Matt. I still think we need to
automatically sort by the 'default_order' of a referenced model
though, when order_by is passed the name of a belongs_to relationship.
I guess the automatic scopes could catch this. e.g. order_by_author
would give something like { :order => "authors.name", :include
=> :author }
Tom
On 15 Sep 2009, at 22:47, Owen wrote:
>
> You got it Matt!
>
> On Sep 15, 7:12 am, Matt Jones <[email protected]> wrote:
>> I haven't poked around in that code enough to tell if this makes
>> sense, but my thought was that we shouldn't be generating a SQL
>> fragment for that at all.
>> Here's an example of what I'd love to be able to do:
>>
>> class SomeModel < AR::Base
>>
>> named_scope :order_by_some_field, lambda { |dir| # code to produce
>> a order scope }
>>
>> or maybe
>>
>> named_scope :order_by_some_field_asc
>> named_scope :order_by_some_field_desc
>>
>> And then the :order_by scope would simply call the correct scope.
>> Automatic scopes would catch the regular cases.
>>
>> This may also solve the "sort on columns not actually present in the
>> original table" thing as well.
>>
>> --Matt Jones
>>
>> On Sep 15, 2009, at 4:34 AM, Tom Locke wrote:
>>
>>
>>
>>> This is a common problem with table-plus.
>>
>>> I've just had a look at the code and I think we can solve this
>>> pretty
>>> easily by having the order_by scope use default_order (if available)
>>> instead of name_attribute.
>>
>>> Something like (from the order_by default scope)
>>
>>> ...
>>> elsif type.respond_to?(:table_name)
>>> include = field
>>> if (order = type.default_order)
>>> colspec = "#{type.table_name}.#{default_order}"
>>> elsif (name = type.name_attribute)
>>> colspec = "#{type.table_name}.#{name}"
>>> end
>>> else
>>> ...
>>
>>> Bryan, Matt? Looks OK?
>>
>>> I guess we'd have to be a bit more clever than this if we want to
>>> support things like "last_name, first_name"
>>
>>> Venka - if this works you'll just be able to add "set_default_order
>>> "last_name" on GrantWriter.
>>
>>> Tom
>>
>>> On 15 Sep 2009, at 09:02, Venka Ashtakala wrote:
>>
>>>> I am using the table-plus tag, and one of the columns in my table
>>>> is
>>>> called 'grant-writer' which isn't a column but actually a model. I
>>>> am
>>>> using the table-plus tag to list Projects and a Project belongs
>>>> to a
>>>> Grant-Writer. The GrantWriter model has a function called 'name'
>>>> but,
>>>> it doesn't have a 'name' field and so there is no 'name' column in
>>>> the
>>>> database. When I include :grant_writer as one of the fields in my
>>>> table-plus tag and I try to sort on it, the generated SQL is
>>>> looking
>>>> for a grant_writer.name column ( which doesn't exist ) and so my
>>>> page
>>>> crashes. Is there any way that I can specify that the 'name'
>>>> column
>>>> should not be used but another column?
>>
>>>> Thanks
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo
Users" 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/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---