On Jueves, 4 de Febrero de 2010 01:24:21 Benjamin Adler escribió:
> I'm new to rails and ActiveScaffold and wondering whether there's an
> elegant way to include columns from an associated model in both a model's
> list/index AND that table's search.
>
> When A belongs_to B and listing A, I would like to include columns B.foo
> and B.bar in the table (and the search-functionality). Right now, I create
> a helper in A's model for every column in B that I want to show, e.g.:
>
> def foo
>  B.foo
> end
>

Another way is add delegated methods to your model.

> And when B.bar is a belongs_to association with model C (B.bar_id pointing
> to a C-id), I'd do e.g.
>
> def bar
>  B.bar.name
> end
>
> In A's controller, I'd have
>
> class A < ApplicationController
>   active_scaffold :a do |config|
>     #config.list.columns.exclude :B
>     config.columns << :foo
>     config.columns << :bar
>
>     config.columns[:foo].search_sql = 'B.foo'
>     config.columns[:bar].search_sql = 'B.bar.name'

That is wrong, B.bar.name is not valid SQL. It should be C.name, where B and C 
are table names for B and C models. Also you should add to the 
joins_for_collection "LEFT JOIN C ON B.bar_id=C.id"

>   end
> end
>
> Not only is this a rather tedious way to add columns, searching for "test"
> does not show a record of A, even though A.B.bar.name is "test". Neither do
> I find a 'WHERE ...name LIKE "%test%"' in script/server's console, so I
> must have misunderstood something.
>
> Why doesn't searching find the record being associated to the
> "test"-record? And, can I maybe write something like

I think you have to include virtual columns to search columns. When you 
include foo and bar columns to search columns you should get a SQL error 
because B.bar.name is not valid in SQL. Fix it with my sugesstions and it 
should work.


-- 
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) [email protected]

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