Reputation: 341
I'm building an application in Ruby on Rails, Rails 4.
Let's say I have a table Users
and I want to specify two types of users User_1
and User_2
User_1
will use all of the columns from the parent table Users
, but User_2
will use all of the columns from the User
table like User_1
but need two extra columns to hold details. The question is where and how are these extra details stored?
Are they columns added to the parent table Users
? or are the extra attributes coded in the Model
or Controller
? I'm looking to store two pieces of integer data, which they will input in their profile.
Thanks for taking a look! :)
Upvotes: 0
Views: 1287
Reputation: 22296
It seams you're in need for the mentioned Single Table Inheritance. But before you proceed I really recommend you to check more information about them. Check this blog post for a more theoretical explanation and how and when to use it.
For a more complete example check this example.
If you still have a pro acount on railscast, there's one about it.
Upvotes: 0
Reputation: 18682
Single Table Inheritance basically means that all subclasses of User
are sharing a single database table, in your case users
. Since you need to store the data in the database, you need to add additional columns to your users
table.
You can find more information on STI in the Rails documentation.
Upvotes: 1
Reputation: 2534
Extra attributes are all stored in the parent table User
. Behind STI scene, there is just one table in your database with an attribute type
telling rails which model should be used. If you want an inherited model to have some columns, it must be set in your parent model, ie. in your database.
Extra attributes are stored in the database, and your model is rails way to help you accessing it.
Upvotes: 0