0

I am trying to generate scaffolding for STI implementation. I issue the following.

rails g scaffold user1 type name email

rails g scaffold member company subscription --parent user1

Every thing gets generated file except for the migration file my 'member' model.
When I try to create a member record like this

Member.create(name: "My Name", email: "myname@example.com", 
              company: 'Example LLC', subscription: 'Monthly Gold' )

I get this error:

ActiveModel::UnknownAttributeError: unknown attribute 'company' for Member. from (irb):1

Any ideas on what is going on?

I use rails 5 and db is postgres

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 1
    Possible duplicate of [Why doesn't 'rails g scaffold' generate a db migration when invoked with a '--parent' argument?](https://stackoverflow.com/questions/34700201/why-doesnt-rails-g-scaffold-generate-a-db-migration-when-invoked-with-a-pa) – biozid bostami Jul 22 '17 at 13:54
  • Yes. This is a duplicate. Thanks. I will modify my ask. – collabrite Jul 22 '17 at 20:11

2 Answers2

0

The --parent option assumes that you are already all setup for single table inheritance, i.e. the parent class has a table with a type column (or whatever column you are using for this).

Since the model will be stored in the parent's table, there is no need to create a new table for the subclass, hence no migration

I got this answer similar to this question asked by someone.

biozid bostami
  • 664
  • 6
  • 13
0

To my understanding, you are on the wrong track. In single table inheritance, all the attributes must be present in the parent model table with an additional column name 'type' to indicate the type of inherited model. The column name 'type' can be changed with appropriate settings but ActiveRecord by default looks for 'type' column. You are getting 'UnknownAttributeError' error cause the parent model does not have the following column in its table. You need to write a migration to add the new columns. Hope you understand the concept of STI. For further exploration, I am providing you the link of the official guide. Hope your problem will be solved. http://edgeguides.rubyonrails.org/association_basics.html#single-table-inheritance

biozid bostami
  • 664
  • 6
  • 13