Archive

Posts Tagged ‘ruby on rails’

Rake Tasks

May 21, 2007 Gaurav Sohoni 1 comment
rake cache:clear                      # Clears all cached pages
rake db:bootstrap                     # Loads a schema.rb file into the database and then loads the initial database fixtures.
rake db:bootstrap:copy_default_theme  # Copy default theme to site theme
rake db:bootstrap:load                # Load initial database fixtures (in db/bootstrap/*.yml) into the current environment's database.  Load specific fixtures using FIXTURES=x,y
rake db:fixtures:load                 # Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y
rake db:migrate                       # Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
rake db:schema:dump                   # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load                   # Load a schema.rb file into the database
rake db:sessions:clear                # Clear the sessions table
rake db:sessions:create               # Creates a sessions table for use with CGI::Session::ActiveRecordStore
rake db:structure:dump                # Dump the database structure to a SQL file
rake db:test:clone                    # Recreate the test database from the current environment's database schema
rake db:test:clone_structure          # Recreate the test databases from the development structure
rake db:test:prepare                  # Prepare the test database and load the schema
rake db:test:purge                    # Empty the test database
rake deploy                           # Push the latest revision into production using the release manager
rake diff_from_last_deploy            # Describe the differences between HEAD and the last production release
rake doc:app                          # Build the app HTML Files
rake doc:clobber_app                  # Remove rdoc products
rake doc:clobber_plugins              # Remove plugin documentation
rake doc:clobber_rails                # Remove rdoc products
rake doc:plugins                      # Generate documation for all installed plugins
rake doc:rails                        # Build the rails HTML Files
rake doc:reapp                        # Force a rebuild of the RDOC files
rake doc:rerails                      # Force a rebuild of the RDOC files
rake edge                             # freeze rails edge
rake log:clear                        # Truncates all *.log files in log/ to zero bytes
rake rails:freeze:edge                # Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)
rake rails:freeze:gems                # Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:unfreeze                   # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:update                     # Update both configs, scripts and public/javascripts from Rails
rake rails:update:configs             # Update config/boot.rb from your current rails install
rake rails:update:javascripts         # Update your javascripts from your current rails install
rake rails:update:scripts             # Add new scripts to the application script/ directory
rake remote_exec                      # Execute a specific action using the release manager
rake rollback                         # Rollback to the release before the current release in production
rake show_deploy_tasks                # Enumerate all available deployment tasks
rake stats                            # Report code statistics (KLOCs, etc) from the application
rake test                             # Test all units and functionals
rake test:functionals                 # Run tests for functionalsdb:test:prepare
rake test:integration                 # Run tests for integrationdb:test:prepare
rake test:plugins                     # Run tests for pluginsenvironment
rake test:recent                      # Run tests for recentdb:test:prepare
rake test:uncommitted                 # Run tests for uncommitteddb:test:prepare
rake test:units                       # Run tests for unitsdb:test:prepare
rake tmp:cache:clear                  # Clears all files and directories in tmp/cache
rake tmp:clear                        # Clear session, cache, and socket files from tmp/
rake tmp:create                       # Creates tmp directories for sessions, cache, and sockets
rake tmp:pids:clear                   # Clears all files in tmp/pids
rake tmp:sessions:clear               # Clears all files in tmp/sessions
rake tmp:sockets:clear                # Clears all files in tmp/sockets
rake update_dialog_helper             # Copies the latest dialog.js to the application's public directory

U can always check the rake tasks list using rake –tasks inside ur rails application.

Categories: rails Tags: , , , , , , ,

Rails Stuff

December 23, 2006 Gaurav Sohoni Leave a comment

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

Generators:

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:

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:
:o rder => ‘name DESC’ # sql fragment for sorting
:o ffset => 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

Migration:

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

Calculations:

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

Categories: rails Tags: , , , ,

Letz Talk Ruby and Rails

December 23, 2006 Gaurav Sohoni Leave a comment

This is about Ruby and of course Rails. So lets get onto Ruby on Rails wagon.

These are some things that I keep using these days:

To check for empty file field:
@image = Image.create params[:image] unless params[:image][:file_data].kind_of? StringIO

here params[:image][:file_data] is the hash for image to be loaded.
If the file field is blank, for :file_data => #<StringIO:0×677cf1c>

Using joins for finding required records:
* A belongs to B

@items = A.find(:all, :conditions => “bs.name like ‘%#{@to_search_for}%’”,
:joins => “left join bs on b_id = bs.id”)
* A belongs to B and B belongs to C

@items = A.find(:all, :conditions => “cs.name like ‘%#{@to_search_for}%’”,
:joins => “left join bs on b_id = bs.id ” +
“left join cs on c_id = cs.id”)
as, bs and cs being the plural forms of a,b and c and hence used as table names.

How to use javascript to go one step back in history pages:
<a href=”javascript:history.go(-1)”>Back</a>

How to add two submit buttons to one form and perform 2 differenr functions:

<%= form_remote_tag :url => {:action => ‘update_info’, :id => @info} %>
<%= render :partial => ‘form’ %>
<%= submit_tag ‘Add Pro’, :name => ‘pro’ %>
<%= submit_tag ‘Add QA’, :name => ‘qa’ %>
<%= end_form_tag %>

check for two submit tag :name attributes in params in controller and do specific task.