Monday, March 3, 2008

RoR: From RyanB's Railscasts

Model.find_all_by_columnName(condition); --or simply find_by_columnName

Class method=> def self.method_name; move multi-use methods from controller to model

with_scope :method_root (ex. :find) => options do
method code
end

when iterating through blocks w/ method--> method to proc --> ex. find(:all).collect(&:name)

virtual attribute--> acts like any column--> define getter and setter methods w/ attribute name in model.
ex.
def full_name
[first_name, last_name].join(' ')
end

def full_name=(name)
split = name.split(' ', 2)
self.first_name = split.first
self.last_name = split.last
end
Named routes--> generic --> map.resources :modelName
custom-->map.custom_name ("route_root/:param1/:param2"), :controller=>controller_name, :action=>action_name

Seven default named routes embedded in map.resources :modelName
  • Index
  • Show
  • New
  • Create
  • Edit
  • Update
  • Destroy

fixtures --> dummy data moves with filesystem

add/remove columns to existing from prompt -->script/generate migration [add/remove]_columnName_[to/from]_tableName

new App--> need DB--> rake db:create

make_resourceful plugin-->
script/plugin install http://svn.hamptoncatlin.com/make_resourceful/tags/make_resourceful

in Controller:
make_resourceful do
actions :all
end

No comments: