This discussion has been extremely helpful.  I just wanted to add a
couple of things in case it helps anyone else:

As Matt said, you can substitute "@klass" with "self".

I wanted to be able to sort on various columns of the associated
table.  This is tricky because the Hobo "order_by" code in
automatic_scopes.rb limits you to sorting on the name column of a
belongs_to associated table (by specifying the relationship name, e.g.
'user', without giving a column, e.g. 'user.age').  To get around this
limitation, Tuishimi eagerly-loaded the associated tables in another
named_scope before applying the sort function.

I am working with Rails 2.3.11 and Oracle so this method works fine
for small sets of data but breaks on larger data sets.  When Rails
does eager loading, it uses a query like this (select * from
related_table where id in
(list_of_distinct_related_ids_from_base_table)).  When my table has
thousands of records, Rails sometimes tries to pull thousands of
records from the related table.  Oracle limits the number of arguments
in an IN clause to 1,000 so I would need to change the Oracle default
limit or use a workaround like the one detailed here
http://www.ruby-forum.com/topic/164681.  I could also try to get the
company to run a solr index for the app but that's not going to happen
at this stage of the project. :)

A quicker solution to the problems with eager-loading large record
sets from Oracle is to add an "order_by" scope to the AR class:

  # INFO: this will die nastily if searching on associations that
don't match table names; need to use reflections
  named_scope :order_by, lambda { |*args|
    field, asc = args
    associated = field.gsub(/\..*$/,'') if field.include?('.')
    field_name = field =~ /^([^.]+)\.(.*)$/ ?
"#{$1.pluralize}.#{$2}" : "#{self.table_name}.#{field}"
    if associated.blank?
      { :order => "#{field_name} #{asc._?.upcase}" }
    else
      { :order => "#{field_name} #{asc._?.upcase}", :include =>
associated }
    end
  }

It's just a slight modification of Matt's "search" named_scope so it
only works as long as the association name matches the table_name.  A
nice side-benefit is that you don't need to manually pluralize
table_names in the dryml template like you do for the Hobo order_by
scope:

    <table-plus fields="user.last_name, user.first_name,
registered_on, graduated_on, inactive" sort-
columns="&{'user.last_name' => 'users.last_name', 'user.first_name' =>
'users.first_name'}">

- Kjersten

On Mar 17, 5:43 pm, Matt Jones <[email protected]> wrote:
> On Mar 17, 2011, at 4:18 PM, Tuishimi wrote:
>
> > Looked like that was a typo?  So I changed the code to be
> > #[email protected]_name} but now I am getting this error:
>
> > undefined method `table_name' for nil:NilClass... it doesn't like
> > @klass...?
>
> Yeah, that's a leftover from the method in automatic_scopes.rb that I used to 
> make that scope - I think the equivalent in a scope would be 'self', but I'm 
> not sure.
>
> --Matt Jones

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

Reply via email to