Run Helper methods in the console
$rails c => helper.number_to_currency(100) => "$100.00" => helper.time_ago_in_words(3.days.ago) => "3 days"Shorthand Migrations
$ rails g resource person first_name:string last_name:string email:string token:string
You can write above command as-
$ rails g resource person first_name last_name email token
Both will generate same migration:
class CreatePeople < ActiveRecord::Migration def change create_table :people do |t| t.string :first_name t.string :last_name t.string :email t.string :token t.timestamps end end end
Add Indexes to migrations
$rails g resource person first_name:index last_name email:uniq token
class CreatePeople < ActiveRecord::Migration def change create_table :people do |t| t.string :first_name t.string :last_name t.string :email t.string :token t.timestamps end add_index :people, :first_name add_index :people, :email, :unique => true end end
Add Associations to migrations
$ rails g resource company person:references name description:text
We can use belongs_to instead of references
$ rails g resource company person:belongs_to name description:text
class CreateCompanies < ActiveRecord::Migration def change create_table :companies do |t| t.references :person t.string :name t.text :description t.timestamps end add_index :companies, :person_id end end
models/company.rb
class Company < ActiveRecord::Base belongs_to :person attr_accessible :description, :name end
Show the status of the database
$ rake db:migrate:status Status Migration ID Migration Name ----------------------------------------- up 20140106131753 Create rule engines down 20140108160604 Add status active to user down 20140109062222 Create people down 20140109063133 Create companies
up : executed
down : not executed yet
Count records in groups
$ rails c KyuEntry.group(:user_id).count {9=>3, 8=>8, 7=>1, 3=>2}
Instantiate Records without Database
=> test\_post = KyuEntry.instantiate("id" => 20, "subject" => "Test", "content" => "instantiate
a object without database")
=> #<KyuEntry id: 20, subject: "Test", content: "instantiate a object without database">
=> test\_post
=> #<KyuEntry id: 20, subject: "Test", content: "instantiate a object without database">
=> KyuEntry.find(20)
=> nil
So this record is not exist in DB
If we want to save then we can use
test_post.save!
Hide comments from the user
<%# ERB comments not visible in html code in the browser %>Home Page
In browser
Home Page
So we should use ERB comments for important documentation in views.
Dropdown with Group Menus
<%= select_tag(:grouped_menu, group_options_for_select( "Group A" => %w[One Two Three], "Group B" => %w[One Two Three]) )%>
Thanks