I have an STI class structure:

class MeteredService < ActiveRecord::Base ; end
class PGEService < MeteredService ; end
class SCEService < MeteredService ; end

In my view, I want update (say) the :resource field:

<%= form_for(metered_service, :url =>
metered_service_path(metered_service)) do |f| %>
  <%= f.text_field 'resource' %>
  <%= f.submit "update" %>
<% end %>

But this generates a params[] hash referring to (e.g.) the PGEService
subclass rather than the MeteredService parent class:

params = {
  "utf8"=>"✓",
  "_method"=>"put",
  "authenticity_token"=>"bAKcTUYbLZyGcMJPyf3zEiyjlB8aQlRv4lqZdiwElhE=",
  "pge_service"=>{"resource"=>"gas"},
  "commit"=>"update",
  "action"=>"update",
  "controller"=>"metered_services",
  "id"=>"54"}

While I could put code in my metered_services_controller to recognize
the subclass:

def update
  @ms = MeteredService.find(params[:id])
  if @ms.update_attributes(params[@ms.class.name.underscore])
  ...
end

... that doesn't smell right to me.  There must be some
Rails-appropriate trick that will let me write the canonical:

def update
  @ms = MeteredService.find(params[:id])
  if @ms.update_attributes(params[:metered_service])
  ...
end

Am I right?  What's the trick?

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" 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/rubyonrails-talk?hl=en.

Reply via email to