Ruby on Rails | Data Base
Database
- IDs columns are mostly generated by rails.
- To create a single migration use this command:
rails generate migration create_tableName
>> e.g. rails generate migration create_articles
after generating migration, you'll need to add column to your table. By adding it to your migration file. When you're finished type in: rake db:migrate
- To create table and everything related to it, you can simply type in this command line:
rails generate scaffold Tablename firstColumn:dataTyp secondColumn:dataTyp
>> e.g. rails generate scafold User username:string email:string
A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
- To migrate your database type in:
rake db: migrate
Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.
- In 'rails console' you can change the data of your database using ruby code. For example, in the 'users' table you can grab any user by 'user_id' using
User.find(userID)
or if you want to toggle any Boolean information in your database, you can use this code: user = User.find(1)
and user.toggle!(:admin)
this code will change admin status of user 1 to true or false. change User and :admin to your need
Validation
- Validations are added in model class.
- Example of validations of presence and length:
validates :username, presence: true, length:{minimum: 3, maximum: 25}
- Example of validations of uniqueness:
validates :email, uniqueness:{case_sensitive:false}
- If you want that all emails are to be formatted to lowercase in your database, simply add
before_save {self.email = email.downcase}
to your User model.
- more example: http://guides.rubyonrails.org/active_record_validations.html
Association
- To create foreign key association when creating scaffold, type in:
rails generate scaffold TableName firstColumn:dataType secondColumn:dataType tableTobeAssociated:references
>> e.g. rails generate scaffold Comment description:text user:references
- Now we know that association has been made between users and comments, we might want to explicit some details of association. for example we want to tell our database that a user can have many comments, we have to add this line to the User model: has_many :comments
- If you want to associate two tables manually, you will have to make some changes to the model of each table. In the User model, we have to add has_many :articles ; and in the Article model, we have to add
belongs_to :user
This will store the userID in your table whenever a comment is made.
Useful command line
- To go to rails console for testing your database
: rails console
- To exit the console:
exit
- To see the existing routes:
rake routes
- To destroy scaffold in case you made mistakes:
rails destroy scaffold TableName
- You have to be in your app directory before using all this command.
No comments:
Post a Comment