-
Notifications
You must be signed in to change notification settings - Fork 9
/
impRORThings
92 lines (66 loc) · 3.2 KB
/
impRORThings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Some basic terminologies:
Active Record Object: It can be described as a table in a database.
How to create a new rails folder?
1. rails new "Folder name"
2. Go to Gemfile and comment out two lines namely:
gem 'spring'
gem 'spring-watcher-listen', '~>2.0.0'
3. Then run bundle update
How to see configuration of the configuration of your website?
bundle exec rake about
How to generate a new controller?
rails generate controller [Controller name] [View name]+
How to generate a new model?
rails generate model [model name] [model attributes]+
Some notable things about models?
How to add a new attribute to a database in rails?(in snake casing)
rails generate migration add_password_digest_to_users password_digest:string
How to remove a attribute to a dabase in rails?(in camel casing)
rails generate migration RemovePartNumberFromProducts part_number:string
How to save the changes in models of rails?
rails db:migrate
How to revert changes in models?
rails db:revert
If you want to start a rails console but do not want to update those changesthen the command would be:
1. rails console --sandbox
How to see attributes of a database?
1. rails console
2. [model name].attribute_names
Update command in Ruby
Address.where(:cid => 15).update_all(user_name: 'Samuel')
If you want to create a new tupple there are two ways:
1. User.create(name: "Sourav", email: "s@g.com")
2. user = User.new(name: "Sourav", email: "s@g.com")
user.save
How to select all the entries of a model?
[model_name].all
How to select first or last entries of a model?
[model_name].first
[model_name].last
If you want to check the full error in validating a variable, then use:
user.errors.full_messages
If you want to find the meaning of any @variable in views then checkout in ap/controllers/specificFolderNameOfView_controller.rb
If you want to add a variable in between a string then use #{variable_name} This is known as escaping interpolation
Any function used in view is written in helper function under the name of the folder of that view:
for example you want to make helper function for a folder named "static_pages" in views folder so you would make function in app/helpers/static_pages_helper.rb
Some functions you can use to implement html elements:
image_tag("path", alt: " ", class: " ")
link_to("body", "link")
has_secure_method of rails adds methods to set and authenticate against a BCrypt password. This mechanism requires you to have a password_digest attribute. The authenticate method automatically authenticates password.
How to do only particular tests?
rake test:functionals
rake test:units
rake test:integration
rake test:plugins - Run all test in ./vendor/plugins/**/test
rake test:recent - Run tests for models and controllers that have been modified in last 10 minutes
rake test:uncommitted - For projects in Subversion, run tests for the changes that took place in the models and controllers since the last commit
bin/rails test test/models/user_test.rb:27 to run a single test
Commenting multiple lines in rails:
#some code
=begin
#code begins
#some code
#code ends
=end
If you want to call a model from controller then use Model.method_name.
If you want to call a model from views you can use @model.method_name.