0

Good Evening Guys,

I have three scaffolds: rails generate scaffold person alter:integer, name:string

rails generate scaffold trainer

rails generate scaffold sportler

 class Person < ActiveRecord::Base
   attr_accessible :alter, :name
 end

 class Sportler < Person
    belongs_to :trainer
 end

 class Trainer < Person
   has_many :sportler
 end

I have added this code in the sportler and trainer view/_form.html.erb

 <div class="field">
     <%= f.label :name %><br />
     <%= f.text_field :name %>
   </div>
  <div class="field">
     <%= f.label :alter %><br />
     <%= f.number_field :alter %>
   </div>

Then I have added some Trainer and Sportsman, after that i take a look in the database. There is one tabel for people, trainers and sportlers. But all entry's are in the peple_table. What goes wrong?

The people table has name and alter as a column. Trainers and Sportlers hasn't these columns.

1
  • When you added some trainers and sportsmen you accidentally added people? Commented Feb 9, 2013 at 23:28

2 Answers 2

1

When you created the scaffolds for Trainer and Sportler they were treated as separate models so they got their own tables in the database. And since you didn't specify any columns they didn't get any.

Then, when you subclass Person for Trainer and Sportler I guess rails assumes that you are using Single Table Inheritance, so they all get stored in the people table. If you want this behaviour, you should add a type column to people I think.

I'm not sure if you could explicitly set the table name in the model (should be something like this): set_table_name "sportlers" and set_table_name "trainers" To get them into their own tables.

Sign up to request clarification or add additional context in comments.

3 Comments

ok sounds good, i tried and get this message. The single-table inheritance mechanism failed to locate the subclass: '0'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Trainer.inheritance_column to use another column for that information.
how can i set type. My form looks like that <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :alter %><br /> <%= f.number_field :alter %> </div> <div class="actions"> <%= f.submit %> </div>
:type can only get set by initializing the object from the class you want. So instead of you specifying the type, Rails does. You can overwrite this by removing :type from the list of protected attributes (usually just :id, :type)
0

To use STI in Rails you need to add the type:string field to your parent model.

rails generate scaffold person alter:integer, name:string, type:string

After that you don't need to scaffold your subclasses. Simply create your new model files and inherit from Person.

Rails is smart enough to use the added type field for persistance of your subclasses. Your subclasses work exactly like an ActiveRecord model. If you run the following code with a subclass:

Trainer.create(name: 'John')

you will receive a new database entry with the type field value of Trainer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.