can you search through multiple levels of association?

3 views
Skip to first unread message

Robin

unread,
Sep 7, 2007, 3:40:29 PM9/7/07
to ActiveScaffold : Ruby on Rails plugin
Anyone know of a method of searching through associations of an
association?

I.e., something along the lines of:

config.columns[:software_packages].search_sql =
"CONCAT(software_packages.name, software_packages.platform.name)"

Bonefish

unread,
Sep 7, 2007, 3:56:30 PM9/7/07
to ActiveScaffold : Ruby on Rails plugin
It's just like the virtual columns.

class SomeThing < ActiveRecord::Base
has_many :outcomes
# add these next 2 so can search on them in field_search
has_many :outcome_types, :through => :outcomes
has_many :categories, :through => :outcomes
end

controller:
config.actions.exclude :search
config.actions << :field_search

#only included in columns to use them for field searching
config.columns.add :outcome_types, :categories
config.list.columns.exclude :outcome_types, :categories
config.create.columns.exclude :outcome_types, :categories
config.update.columns.exclude :outcome_types, :categories
config.show.columns.exclude :outcome_types, :categories

config.columns[:outcome_types].search_sql = 'outcome_types.name'
config.columns[:categories].search_sql = 'project_types.name'
config.columns[:categories].label = 'Outcome Categories'

So you'll have 2 columns on the field search form:
Outcome Types and Outcome Categories

Robin

unread,
Sep 7, 2007, 4:35:10 PM9/7/07
to ActiveScaffold : Ruby on Rails plugin
Cool. So, is there a way to do this in the case of belongs_to (we
can't do belongs_to :through right?)

Bonefish

unread,
Sep 7, 2007, 5:07:26 PM9/7/07
to ActiveScaffold : Ruby on Rails plugin
I think the only way to do that would be a database view of the parent
and it's associations so that the association data becomes
denormalized. Then the association data can be accessed simply via a
virtual column (since it's now just a column of the parent). Of
course then you'd need different classes for reading versus updating
the data and/or have to do some behind the scenes class substitution
in the CRUD actions. Use the view class for list and show but the
real parent for create and update.

Bonefish

unread,
Sep 8, 2007, 1:11:04 AM9/8/07
to ActiveScaffold : Ruby on Rails plugin
To clarify - that's to get to has_many associations of a parent
record. If it's just a belongs to on the parent then you can get to
that with a virtual column.

Kerry Foley

unread,
Sep 29, 2008, 3:56:47 PM9/29/08
to actives...@googlegroups.com, joelm
Can you try it with a new controller first - named for the view? That
will make sure that all the database permissions on the view, naming,
pluralization etc is properly set up. I don't think ActiveScaffold
distinguishes between tables and views so it shouldn't have a problem.

If that works then having a separate controller for the report might be
the easiest solution. If you want to intermingle them this is quite a
bit more work. Since views are read-only you will have have to override
activescaffold's CRUD actions as described below. Plus you forevermore
will have to keep them in sync.

Regards,
Kerry

joelm wrote:
> Hi-
>
> A year ago, you gave this reply to the ActiveScaffold group. I'm
> emailing since I don't know if you'd see an update to such an old
> thread.
> Anyway, I tried to follow your advice, created a view and created an
> model for the view. (The model is empty for now, no
> associations. )
> But when I point my controller at the new model I get:
>
> Mysql::Error: #42S02Table
> 'rgame_development.view_report_grouping_titles' doesn't exist: SHOW
> FIELDS FROM `view_report_grouping_titles`
>
> It seems that with MySql ActiveScaffold can't use a view
> interchangeably with a table.
>
> (The problem I'm really trying to solve is that I want to do a 'group
> by' in my data before reporting, so my plan was to use
> a view to setup the groupby since AS doesn't allow us to pass
> in :group as part of the query.)
>
> Thanks for any advice!
>
> Joel


>
> On Sep 8 2007, 1:11 am, Bonefish <bonefis...@gmail.com> wrote:
>
>> To clarify - that's to get to has_many associations of a parent
>> record. If it's just a belongs to on the parent then you can get to
>> that with a virtual column.
>>
>> On Sep 7, 2:07 pm, Bonefish <bonefish.fo...@gmail.com> wrote:
>>
>>

>>> I think the only way to do that would be adatabaseviewof the parent


>>> and it's associations so that the association data becomes
>>> denormalized. Then the association data can be accessed simply via a
>>> virtual column (since it's now just a column of the parent). Of
>>> course then you'd need different classes for reading versus updating
>>> the data and/or have to do some behind the scenes class substitution

>>> in the CRUD actions. Use theviewclass for list and show but the

Reply all
Reply to author
Forward
0 new messages