Things which Rails could do but I didn’t know.

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

<!-- HTML comments stay visisble to the user -->
<%# ERB comments not visible in html code in the browser %>
<h1>Home Page</h1>

In browser

<body>
<!-- HTML comments stay visisble to the user -->
<h1>Home Page</h1>
</body>

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s