| Start |
|---|
| Creating a Model via generator Ruby on Rails provides a In addition to the model file in
You can also generate some fields for the model when generating it. This will create the columns email, sign_in_count and birthday in your database, with the appropriate types. |
| Creating a Model manually While using scaffolding is a fast and easy if you are new to Rails or you are creating a new application, later it can be useful just to do it on your own ato avoid the need to go through the scaffold-generated code to slim it down (remove unused parts, etc.). Creating a model can be as simple as creating a file under The most simple model, in Model files are stored in The class will inherit all the ActiveRecord features: query methods, validations, callbacks, etc. Note: Make sure that the table for the corresponding model exists. If not, you can create the table by creating a Migration (opens new window) You can generate a model and it's migration by terminal from the following command
and can also assign foreign key(relationship) to the model by following command |
| Introduction to Callbacks A callback is a method that gets called at specific moments of an object's lifecycle (right before or after creation, deletion, update, validation, saving or loading from the database). For instance, say you have a listing that expires within 30 days of creation. One way to do that is like this: All of the available methods for callbacks are as follows, in the same order that they are called during the operation of each object: Creating an Object
Updating an Object
Destroying an Object
NOTE: after_save runs both on create and update, but always after the more specific callbacks after_create and after_update, no matter the order in which the macro calls were executed. |
| Manually Testing Your Models Testing your Active Record models through your command line interface is simple.
Navigate to the app directory in your terminal and type in For example, if you had a database schema with a Users table having a Then, to show that record, you could run: Or if this is your first or only record, you could simply get the first record by running: |
Creating A Migration Add/remove fields in existing tablesCreate a migration by running: This will create a migration that adds a Similarly, you can generate a migration to remove a column: This will create a migration that removes a While, strictly speaking, specifying type ( Create a tableCreate a migration by running: Rails recognizes the intent to create a table from the Notice that the creation command didn't specify types of columns and the default Create a join tableCreate a migration by running: Rails detects the intent to create a join table by finding Uncomment the necessary PrecedenceNotice that the example migration name |
| Create a Join Table using Migrations Specially useful for The actual name of the table needs to follow this convention: the model which alphabetically precedes the other must go first. Project preceds Tags so the name of the table is projects_tags. Also since the purpose of this table is to route the association between the instances of two models, the actual id of every record in this table is not necessary. You specify this by passing Finally, as is convention in Rails, the table name must be the compound plural form of the individual models, but the column of the table must be in singular form. |
| Using a model instance to update a row Let's say you have a Now to update the Calling |
Read Article
https://develop.horje.com/learn/1434/reference