Where I had a complex situation -- though perhaps less complex as what
you're after -- I successfully used named scopes instead of hobo's
apply_scopes. In my case I have a table-plus showing custodians on my
project page, and wanted to make the search box find custodians.
In my case, I added two named scopes to my join class
(ProjectCustodian):
named_scope :custodian_names_like, lambda { |search_for| (!
search_for.blank?) ? {:include => :custodian, :conditions =>
["custodians.name LIKE ?", "%#{search_for}%"] } : nil }
named_scope :sort_custodians_by, lambda { |sort_by|
if !sort_by.blank?
# fix custodian --> custodians
klass=sort_by.first.split(".").first
sort_by.first.gsub!(klass, klass.pluralize)
{:include => :custodian, :order => sort_by.join(' ')}
else
nil
end
}
and this is how I used them from my ProjectsController:
def edit
hobo_show do
@project = find_instance
order = parse_sort_param("custodian.name", "custodian.type",
"custodian.last_name")
@project_custodians =
@project.project_custodians.custodian_names_like(params
[:search]).sort_custodians_by(order)
end
end
On Jun 24, 10:19 am, hobo_hippy <[email protected]> wrote:
> So Montgomery Kosma,
>
> def index
> @timesheets = Timesheet.apply_scopes(:search => [params
> [:search], :name], :order_by => parse_sort_param
> (:name, :time, :client, :project, :user, :updated_at))
> hobo_index @timesheets
>
> I've got one little modification I'd like to make. the search only
> looks at :name becuase that's stated here:
>
> (:search => [params[:search], :name]
>
> if I want to be able to search :name, :client, :project, and :user how
> can I modify this? i tried just adding the fields, but that doesn't
> work since client, project, and user are not @Timesheets, where I
> already told it to look. Do I have to write another search method
> inside index? How can I make it search across all models instead of
> one model?
>
> btw I get the following error message:
>
> Mysql::Error: Unknown column 'timesheets.user' in 'where clause':
> SELECT * FROM `timesheets` WHERE (((timesheets.name like '%CJ%') OR
> (timesheets.user like '%CJ%') OR (timesheets.client like '%CJ%') OR
> (timesheets.project like '%CJ%')))
>
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/abstract_adapter.rb:188:in `log'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/mysql_adapter.rb:309:in `execute'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/mysql_adapter.rb:563:in `select'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/abstract/database_statements.rb:7:in
> `select_all_without_query_cache'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/abstract/query_cache.rb:60:in `select_all'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/abstract/query_cache.rb:81:in `cache_sql'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> connection_adapters/abstract/query_cache.rb:60:in `select_all'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> base.rb:635:in `find_by_sql'
> /usr/lib/ruby/gems/1.8/gems/hobo-0.8.7/lib/hobo/model.rb:293:in
> `find_by_sql'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> base.rb:1490:in `find_every'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> base.rb:589:in `find'
> /usr/lib/ruby/gems/1.8/gems/hobo-0.8.7/lib/hobo/model.rb:286:in `find'
> /usr/lib/ruby/gems/1.8/gems/hobo-0.8.7/lib/hobo/scopes/
> named_scope_extensions.rb:20:in `send'
> /usr/lib/ruby/gems/1.8/gems/hobo-0.8.7/lib/hobo/scopes/
> named_scope_extensions.rb:20:in `method_missing'
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
> base.rb:2003:in `with_scope'
> (__DELEGATION__):2:in `__send__'
> (__DELEGATION__):2:in `with_scope'
> (__DELEGATION__):2:in `is_a?'
> app/controllers/timesheets_controller.rb:9:in `index'
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---