On Viernes, 12 de Noviembre de 2010 12:59:22 odigity escribió: > Thanks for providing a practical solution. Having a little trouble > getting it to work, though. > > It seems that the account_entity_id_form_column method isn't getting > called. I put a line in there that logs a test message, and it's not > showing up in the output.
Do you get entity_id in your form? If you don't get it, add entity_id to config.columns. > > I have ActiveScaffold set up under an admin/ subdirectory, so the > controller is: > /app/controllers/admin/accounts_controller.rb > > Just to be certain I got the location and naming right, I created two > helper modules, and pasted the method into both: > /app/helpers/accounts_helper.rb (Module AccountsHelper) > /app/helpers/admin/accounts_helper.rb (Module Admin::AccountsHelper) You have to use admin/accounts_helper if your controller is inside admin directory. > > Neither of them are being invoked. > > BTW-Do I also need to add this? > > > config.columns[:entity_id].form_ui = :select No, because you need to set options for select dynamically, so you have to use a form override and then form_ui is not used. > > Seems to make sense to me, since we're trying to make it a drop-down > box, but AS freaks out when do it. > > Lastly, a theoretical question: Why couldn't AS be modified to look > at the models that are associated with :account as entities? Doesn't > Rails have both directions of the association available for > introspection? No, rails has no reverse association. AS tries to guess reverse association using class_name, but it's not possible with polymorphic association (it would try to get Entity class). > Then AS could go and select all records in both tables > and include them in the drop-down box. That's ultimately what I'd > prefer, and would be some awesome default logic to include. It won't work, you won't be able to fill type and id foreign keys only with a form, you won't know what class is selected. Two chained select tags are needed. You could build a select tag with both tables setting a custom value which joins class name and id, and you should fill type and id fields in the controller, but it must be custom code for each app. > > -ofer > > On Nov 12, 3:35 am, "Sergio Cambra .:: entreCables S.L. ::." > > <[email protected]> wrote: > > On Jueves, 11 de Noviembre de 2010 16:44:15 odigity escribió: > > > Kinda new here. > > > > > > For this example, let's assume that I have three models: > > > > > > Person > > > has_many :accounts, :as => :entity > > > Organization > > > has_many :accounts, :as => :entity > > > Account > > > belongs_to :entity, :polymorphic => true > > > > > > I can't create any accounts because ActiveScaffold won't display a > > > drop-down box on the create page with a list of people and > > > organizations, therefore I can't associate the account with an entity. > > > > > > What's the best way to deal with this? > > > > ActiveScaffold can't guess what classes can act as entity. > > The best way is add a field for entity_type and another for entity_id. > > Set :select form_ui to entity_type, with Person and Organization as > > options. Then add a form override for entity_id: > > > > config.columns[:entity_type].form_ui = :select > > config.columns[:entity_type].options[:options] = ['Person', > > 'Organization'] config.columns[:entity_type].update_column = :entity_id > > > > #AccountsHelper > > def account_entity_id_form_column(record, options) > > select_options = if record.entity_type > > record.entity_type.constantize.all.map {|r| [r.to_label, r.id]} > > else > > [] > > end > > select :record, :entity_id, select_options, options > > end > > > > -- > > Sergio Cambra .:: entreCables S.L. ::. > > Mariana Pineda 23, 50.018 Zaragoza > > T) 902 021 404 F) 976 52 98 07 E) [email protected] -- 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.
