On Thursday 04 February 2010 09:34:14 Sergio Cambra .:: entreCables S.L. ::. 
wrote:
> > def foo
> >  B.foo
> > end
> 
> Another way is add delegated methods to your model.

Whee, I like delegated methods! I added lots of delegations to A's model, 
e.g.:

delegate :bar, :to => :B

But even with A's controller like

config.columns << :bar

def joins_for_collection
  "LEFT JOIN C ON B.bar_id=C.id"
end

the two problems remain:

a) instead of showing C's name in the table, I get values like 
"#<C:0x7f63f3dc4ed0>" in the cells, and I don't know how to specify that not 
"C", but rather "C.name" should be displayed

b) When searching, the extension specified in joins_for_collection (is that 
override documented somewhere? google isdn't really helpful) seems to be 
ignored. At least, I don't see the corresponding SQL being passed to mysql in 
script/server's log. What is joins_for_collection supposed to return, an array 
with strings? If not, how do I return multiple joins?

Also, what would a join_for_collection for a habtm-association look like? In 
my case, B also habtm D, how can I show all Ds in A's table? Sorry to ask 
these probably basic questions, but I couldn't find any documentation on 
joins_for_collection.

Just for completeness, here are my models and controllers. A is 
available_tubes, B ist catalog_items, C is conjugates (and D is reactivities, 
but other than being habtm its really not worth showing)

class AvailableTube < ActiveRecord::Base
  belongs_to :catalog_item
  belongs_to :user
  belongs_to :storage, :class_name => '::Storage'
  delegate :conjugate, :manufacturer, :to => :catalog_item

  def name
    catalog_item.name + " - " + user.name + " - " + storage.name
  end
end

class AvailableTubesController < ApplicationController
  active_scaffold :available_tube do |config|
    config.columns << :conjugate
    config.columns[:conjugate].search_sql = 'conjugate.name'

    def joins_for_collection
      "LEFT JOIN conjugates ON catalog_items.conjugate_id=conjugate.id"
    end
  end
end


class CatalogItem < ActiveRecord::Base
  belongs_to :conjugate
  belongs_to :manufacturer
  has_many :available_tubes
  has_and_belongs_to_many :reactivities
  validates_presence_of :conjugate

  def name
    conjugate.name + " - " + manufacturer.name
  end
end

class CatalogItemsController < ApplicationController
  active_scaffold :catalog_item do |config|
    config.columns[:conjugate].form_ui = :select
    config.columns[:reactivities].form_ui = :select
    config.nested.add_link("Available Tubes", [:available_tubes])
  end
end


class Conjugate < ActiveRecord::Base
  has_many :catalog_items
  validates_presence_of :name
end

class ConjugatesController < ApplicationController
  active_scaffold :conjugate do |config|
    config.nested.add_link("Catalog Items", [:catalog_items])
    config.list.columns.exclude :catalog_items
  end
end

Thank you, Sergio!
ben

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

-- 
Benjamin Adler
Heidberg 22
22301 Hamburg
0160 / 858 0660

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