I’ve got a model client, with single table inheritance.
But the when I try to submit the form, the type field doesn’t get saved in the database. How can I force it to save the type and then display the account type on index.html.erb.
models/client.rb
class Client < ActiveRecord::Base
end
class Suscriber < Client
end
class NonSuscriber < Client
end
views/_form.html.erb
<%= simple_form_for @client do |f| %>
<%= f.input :name %>
<%=f.input :type %>
<%= f.button :submit %>
<% end %>
clients_controller.rb
def index
@clients = Client.where(:type => params[:type])
respond_to do |format|
format.html
format.json {render json: @clients}
end
end
def new
@client = Client.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @client }
end
end
def create
@client = Client.new(params[:client])
respond_to do |format|
if @client.save
format.html { redirect_to @clinet, :notice => 'Client was successfully created.' }
format.json { render :json => @client, :status => :created, :location => @client }
else
format.html { render :action => "new" }
format.json { render :json => @client.errors, :status => :unprocessable_entity }
end
end
end
I’m on rails 3.1
The docs say:
“Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column).”
As mentioned in the doc, you need to use
set_inheritance_column, have a look at http://apidock.com/rails/v3.1.0/ActiveRecord/Base/set_inheritance_column/classHTH