Rails Notes

Check empty, nil, blank

Formatted JSON

Pass parameters between new and create actions
Use a hidden field tag
hidden_field_tag :data, @data

Render partial with collection

render :partial => "product", :collection => @products 

Simple search

def self.search(search)
search_condition = "%" + search + "%"
find(:all, :conditions => ['title LIKE ? OR description LIKE ?', _
search_condition, search_condition])

Another way
SomeModel.where("field LIKE ?", params[:search]).first

validate presence of one or the other

validates :email, :presence => {:unless => "phone", _
:message => "Please enter either an email or a phone number"}

data types for rails

:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.

 add fields to existing table

rails generate migration add_fieldname_to_tablename fieldname:string

test a controller actions using Rails console

app.get '/controller/action'

for passing parameters use


for checking the response


Convert a list in to comma separated string 

tags.map { |tag| tag.name }.join(',')

© 2014. All rights reserved.