add favicon

http://stackoverflow.com/questions/9901910/how-to-add-favicon-in-rails-3-2

Simply add this to the <head></head> section of your layouts app/view/layouts/application.html.erb:

<%= favicon_link_tag 'favicon.ico'%>

Place the favicon.ico image in /app/assets/images/ if you are using the asset pipeline, and in /public/images/ if you are not.

rake in rails

Rake is a utility similar to make in Unix. You can say Rake is the make of ruby – the R uby m AKE. Rails defines a number of tasks to help you. http://www.tutorialspoint.com/ruby-on-rails/rails-and-rake.htm

http://guides.rubyonrails.org/command_line.html

Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility ‘make’, and uses a ‘Rakefile’ and .rake files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other. ( rake tasks are defined in Rails.)

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks. Each task has a description, and should help you find the thing you need.

 

to_json and as_json

If you want to change the root; for example modify the classname when calling to_json on an AR model.

i.e.

Book.first.to_json
 #=> "{\"book\":{\"created_at\":\"2010-03-23 

Book.first.to_json(:root => 'libro')
 #=> "{\"libro\":{\"created_at\":\"2010-03-23 

Note that :root is not available before Rails 3.0.5

To be compatible with Rails 3, override as_json instead of to_json. It was introduced in 2.3.3:

def as_json(options={})
  { :libro => { :created_at => created_at } }
end

Make sure ActiveRecord::Base.include_root_in_json = false. When you call to_json, behind the scenes as_json is used to build the data structure, and ActiveSupport::json.encode is used to encode the data into a JSON string.

migration review

  • rake db:migrate VERSION=20080906120000 ( if it is run already,  won’t be run again)
  • rake db:migrate:up VERSION=20080906120000
  • rake db:migrate:redo STEP=3
  • rake db:migrate:redo VERSION=20080906120000
  • rake db:rollback This will run the down method from the latest migration ( check out scheme_migrations to see which one is the latest; if many people are working at the same time, you will be surprised about the result. If the latest migration file is not on your desktop, it won’t run anything.)

rails cache

http://guides.rubyonrails.org/caching_with_rails.html

http://blog.blazingcloud.net/2010/11/21/caching-in-rails/

  • difference between caches_page and caches_action: the former just caches the page content to a file, the latter called some before filters in the action before do the cache ( the action itself won’t be called, just its before filters).
  • expire_page, expire_action
  • config.action_controller.perform_caching = true ( be careful if you are in development env; it will do the cache and your action change will NOT have effect immediately. I would set it to be false in development env.)

select and pluck

For example, to select only id columns:
Client.select(“id”)

The SQL query used by this find call will be somewhat like:
SELECT id FROM clients

Be careful because this also means you’re initializing a model object with only the fields that you’ve selected. If you attempt to access a field that is not in the initialized record you’ll receive:

example:

a = Person.select(:name).uniq
=> [#<Person name: “Mary”>, #<People name: “John”>]
irb(main):011:0> a.first.name
=> “Mary”
irb(main):012:0> a.first.address
ActiveModel::MissingAttributeError: missing attribute: address

pluck can be used to query a single column from the underlying table of a model. It accepts a column name as argument and returns an array of values of the specified column with the corresponding data type.

select returns a Model object with only :column populated
pluck only returns the values
select(:id) will return an array of Model
pluck(:id) will return an array of integers ( suppose the column id has integer type)

rails console reload

Since you don’t want to restart your console every time to pick up the changes, you can do

  • Load only one file that you made change to, in the console, type

load ‘app/model/your_file_name.rb’

  • load all changes

http://stackoverflow.com/questions/5280419/is-the-rails-console-dynamic

You will need to call the reload! method in the console to reload the changes. This method’s magic is automatically called by rails server in development mode.

As a comment’s pointed out beneath and another answer here, if you change things to do with the environment of the application, such as adding new gems to the Gemfile, making changes to anything in config or adding a new plugin then you’ll need to restart the console. Any changes to app will be reloadable with reload!

If you were using this particular way to test that a method was working, I wouldn’t. Tests (as in, the Test::Unit or RSpec) variants are much nicer because you have a reproducible way of running them again and again. rails console is great for one-off testing, but if you want to write a maintainable application then write tests.