简体   繁体   中英

Classes and models in ruby on rails

Is creating classes and creating models are same. For example if I had to make a product class, so I have to make a product model for same?

Yes, if you need a Product Class you create a Product Model. Once you created Product Model, Product Class is available.

Model is a little bit complex entity than just a class. Model in rails is representation of Active Record. Here its contents:

Active Record gives us several mechanisms, the most important being the ability to:
Represent models and their data.
Represent associations between these models.
Represent inheritance hierarchies through related models.
Validate models before they get persisted to the database.
Perform database operations in an object-oriented fashion.

In order to create order, you will need to create a table (rails uses migrations for this case), and model class in this example product . You can use rails generators to achieve this, the following will create both model and migration

bin/rails generate model Product name:string description:text

further reading.
Active Record Model Generators

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM