Rails Stuff

Some useful rake commands:

rake migrate version=0
rake test:units
rake test:functionals
rake annotate_models
rake db:sessions:clear
rake doc:app
rake log:clear

rake db:test:prepare -> prepares test database with development database schema

rake -T #displays all rake tasks with description


ruby script/generate model ModellName
ruby script/generate controller ListController show edit
ruby script/generate scaffold ModelName ControllerName
ruby script/generate migration AddNewTable
ruby script/generate plugin PluginName
ruby script/generate mailer Notification lost_password signup
ruby script/generate web_service ServiceName api_one api_two
ruby script/generate integration_test TestName
ruby script/generate session_migration


find(42) # object with ID 42
find([37, 42]) # Array with the objects with id 37, 42
find :all
find :first,
:conditions => [ “name = ?”, “Hans” ] # finds the first record with
# the matching condition

more parameters for find:
:order => ‘name DESC’ # sql fragment for sorting
:offset => 20 # starts with entry 20
:limit => 10 # only return 10 objects
:group => ‘name’ # sql fragment GROUP BY
:joins => ‘LEFT JOIN …’ # additional LEFT JOIN (rarely used)
:include => [:account, :friends] # LEFT OUTER JOIN with these model
:include => { :groups => { :members=> { :favorites } } }
:select => [:name, :adress] # instead of SELECT * FROM
:readonly => true # objects are write protected


rake db:migrate
rake db:migrate VERSION=14
rake db:migrate RAILS_ENV=production


Person.average :age
Person.minimum :age
Person.maximum :age
Person.sum :salary, :group => :last_name


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s