MVC design

if the logic can be done without variables from view, then it should be in model; if the logic must be done not only model’s data but also views’ states, then it should be in controller


add favicon

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.

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.


 #=> "{\"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 } }

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.)