SlideShare a Scribd company logo
1 of 37
Amazing things in Rails
@ka8725
Load gems precedence
config/application.rb:
config.plugins = [:devise, :i18n, :all]




* default precedence is alphabetical
Changing backtrace
config/initializers/backtrace_silencer.rb:
Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
Rails.backtrace_cleaner.remove_silencers!
List of loaded gems
$ rails c
>> $LOAD_PATH
=>[“/usr/local/lib/ruby/...”, ...]
Log to syslog

Allows to log to the remote server

http://docs.seattlerb.org/SyslogLogger/
Log to syslog
Allows to log to the remote server
http://docs.seattlerb.org/SyslogLogger/
Redirecting routes
config/routes.rb:
match ‘/google’, :to => redirect(‘http://google.com’)
Collect complicated routes
config/routes.rb:
match ‘items/list/*specs’, :controller => ‘items’, :action => ‘list’

/items/list/base/books/fiction/dickens/little_dorrit

params[:specs] # => “base/books/fiction/dickens/little_dorrit”
List of controller routes
$ rake routes CONTROLLER=products
Will show the list of ProductsController routes
Preview new record route
resources :reports do
new do
     post :preview
end
end

preview_new_report POST /reports/new/preview(.:format) {:action => ‘preview’,
:controller => ‘reports’}


= form_for(report, :url => preview_new_report_path) do |f|
...
= f.submit ‘Preview’
Avoid member actions
resources :bids do
resource :retraction
end
Verify instead of before_filter

verify :params => ‘privileges’, :only => :update, :redirect_to => {:action => ‘settings’}




* Redirects to setting action unless privileges param key presents
Read attributes before type
casting
<attribute_name>_before_type_cast




* Example: @user.phone_before_type_cast
Read only attributes
class Customer < ActiveRecord::Base
attr_readonly :social_security_number
end
Get random record

Timesheet.limit(1).offset(rand(Timesheet.count)).first
Select all attributes with
calculated values
BilableWeek.select(:*, “mon_hrs + tues_hrs as two_day_total”)




* Pay attention on :*
Method “from” in the selects
to join tables or views
BilableWeek.select(“...”).from(“users”).where(“users.name = ‘John’”)
Method ‘exists?’ in AR

User.exists?(1)

User.find_by_id(1).present?
Use Time instead of DateTime

Time is faster because it is written in C
Redo migration

rake db:migrate:redo


It’s the same as:

rake db:rollback
rake db:migrate
Difference between << and
create on the associations
<< is transactional, but create is not
<< triggers :before_add and :after_add callbacks but create
doesn’t
Method ‘clear’ on the
associations
Transactionally removes all records and triggers callbacks
Reloading associations

User.locations(true)
insert_sql and delete_sql
options for associations
Convenient way to avoid callbacks




* Please, reference to rails doc
:autosave => true

class User < AR
has_many :timesheets, :autosave => true
end

user = User.first
user.timesheets.first.mark_for_destriction
user.save # will delete timesheet record
validates_acceptance_of

 class Account < AR
   validates_acceptance_of :privacy_policy, :terms_of_service
 end



Creates virtual attributes automatically
validates on
class Report < AR
validates_presence_of :name, :on => :publish
end

report.valid? :publish




Validators for specific fields
Merging scopes

scope :tardy, :lambda {
joins(:timesheets).group(“users.id”) & Timesheet.late
}




For this purpose you are able to use merge method too
Callback classes
class MarkAsDeleted
  def self.before_destroy(model)
    model.update_attribute(:deleted_at, Time.now)
    false
  end
end

class Account < AR
  before_destroy MarkAsDeleted
end

class Invoice < AR
  before_destroy MarkAsDeleted
end
Callback classes
class MarkAsDeleted
  def self.before_destroy(model)
    model.update_attribute(:deleted_at, Time.now)
    false
  end
end

class Account < AR
  before_destroy MarkAsDeleted
end

class Invoice < AR
  before_destroy MarkAsDeleted
end
Calculation methods
#average
#count
#maximum
#minimum
#sum

Person.calculate(:count, :all)
Person.average(:age)
Person.minimum(:age).where(‘last_name <> ?’, ‘John’)
STI another column



set_inheritance_column ‘not_type’
Check defined local variables in
partials


local_assigns.has_key? :special
Entry counter in the partials


= div_for(entry) do
  span ##{entry_counter}
  span #{entry.name}
Many asset hosts


 config.action_controller.asset_host = Proc.new { |source|
   “http://assets#{rand(2) + 1}.example.com”
 }
number_to_phone method



 number_to_phone(1235551234) #=> “123-555-1234”
auto_link method

 auto_link(“Go to http://google.com. Email:
 test@email.com”)

 => Go to <a
 href=”http://google.com”>http://google.com</a>. Email: <a
 href=”mailto:test@email.com”>text@email.com</a>

More Related Content

What's hot

Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideVisual Engineering
 
Binary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel ControllersBinary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel ControllersBinary Studio
 
Intro to Rails
Intro to Rails Intro to Rails
Intro to Rails epiineg1
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Umair Amjad
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?Tomasz Bak
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriAbdul Malik Ikhsan
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHPAndru Weir
 
OSC2007-niigata - mashup
OSC2007-niigata - mashupOSC2007-niigata - mashup
OSC2007-niigata - mashupYuichiro MASUI
 
Curing Webpack Cancer
Curing Webpack CancerCuring Webpack Cancer
Curing Webpack CancerNeel Shah
 
MVest Spring Job Execution
MVest Spring Job ExecutionMVest Spring Job Execution
MVest Spring Job ExecutionShraddha Bora
 
Swift Delhi: Practical POP
Swift Delhi: Practical POPSwift Delhi: Practical POP
Swift Delhi: Practical POPNatasha Murashev
 
XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話Takehito Tanabe
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareVisual Engineering
 
Power shell examples_v4
Power shell examples_v4Power shell examples_v4
Power shell examples_v4JoeDinaso
 
Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)Natasha Murashev
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersCaldera Labs
 

What's hot (20)

Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Binary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel ControllersBinary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel Controllers
 
getSIDUsers
getSIDUsersgetSIDUsers
getSIDUsers
 
Simple acl with laravel
Simple acl with laravelSimple acl with laravel
Simple acl with laravel
 
Intro to Rails
Intro to Rails Intro to Rails
Intro to Rails
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?
 
fabfile.py
fabfile.pyfabfile.py
fabfile.py
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate Uri
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHP
 
OSC2007-niigata - mashup
OSC2007-niigata - mashupOSC2007-niigata - mashup
OSC2007-niigata - mashup
 
Curing Webpack Cancer
Curing Webpack CancerCuring Webpack Cancer
Curing Webpack Cancer
 
Laravel
LaravelLaravel
Laravel
 
MVest Spring Job Execution
MVest Spring Job ExecutionMVest Spring Job Execution
MVest Spring Job Execution
 
Swift Delhi: Practical POP
Swift Delhi: Practical POPSwift Delhi: Practical POP
Swift Delhi: Practical POP
 
XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
Power shell examples_v4
Power shell examples_v4Power shell examples_v4
Power shell examples_v4
 
Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress Developers
 

Viewers also liked

Presentation147to158
Presentation147to158Presentation147to158
Presentation147to158andreahdzq
 
Evaluation question 2
Evaluation question 2Evaluation question 2
Evaluation question 2jamiegibbins
 
Baa adab baa muhaawara hoshiyaar ( Urdu humor) طنز ومزاح
Baa adab baa muhaawara hoshiyaar ( Urdu humor)  طنز ومزاحBaa adab baa muhaawara hoshiyaar ( Urdu humor)  طنز ومزاح
Baa adab baa muhaawara hoshiyaar ( Urdu humor) طنز ومزاحtabdeeli
 
Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890a1597
 
Question 2 from evaluation
Question 2 from evaluationQuestion 2 from evaluation
Question 2 from evaluationjamiegibbins
 
Question 7 from evaluation
Question 7 from evaluationQuestion 7 from evaluation
Question 7 from evaluationjamiegibbins
 
Корпоративное приложение на Rails
Корпоративное приложение на RailsКорпоративное приложение на Rails
Корпоративное приложение на RailsAndrei Kaleshka
 
Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890a1597
 
Живые обои для Android. Как создать. Тонкости. Продвижение
Живые обои для Android. Как создать. Тонкости. ПродвижениеЖивые обои для Android. Как создать. Тонкости. Продвижение
Живые обои для Android. Как создать. Тонкости. ПродвижениеSergey Mikhaylov
 
Atlas an overview sept2012
Atlas   an overview sept2012Atlas   an overview sept2012
Atlas an overview sept2012rahul_quantech
 
Atlas an overview sept2012
Atlas   an overview sept2012Atlas   an overview sept2012
Atlas an overview sept2012rahul_quantech
 
Food security bill
Food security billFood security bill
Food security billPANKAJMISHR
 

Viewers also liked (18)

Presentation147to158
Presentation147to158Presentation147to158
Presentation147to158
 
Evaluation question 2
Evaluation question 2Evaluation question 2
Evaluation question 2
 
Baa adab baa muhaawara hoshiyaar ( Urdu humor) طنز ومزاح
Baa adab baa muhaawara hoshiyaar ( Urdu humor)  طنز ومزاحBaa adab baa muhaawara hoshiyaar ( Urdu humor)  طنز ومزاح
Baa adab baa muhaawara hoshiyaar ( Urdu humor) طنز ومزاح
 
Location shots
Location shotsLocation shots
Location shots
 
Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890
 
Exam
ExamExam
Exam
 
Question 2 from evaluation
Question 2 from evaluationQuestion 2 from evaluation
Question 2 from evaluation
 
Ruby exceptions
Ruby exceptionsRuby exceptions
Ruby exceptions
 
Question 7 from evaluation
Question 7 from evaluationQuestion 7 from evaluation
Question 7 from evaluation
 
Корпоративное приложение на Rails
Корпоративное приложение на RailsКорпоративное приложение на Rails
Корпоративное приложение на Rails
 
Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890Basic format robeertaaa carrillooo5656767897890
Basic format robeertaaa carrillooo5656767897890
 
Rails 3 assets pipeline
Rails 3 assets pipelineRails 3 assets pipeline
Rails 3 assets pipeline
 
Живые обои для Android. Как создать. Тонкости. Продвижение
Живые обои для Android. Как создать. Тонкости. ПродвижениеЖивые обои для Android. Как создать. Тонкости. Продвижение
Живые обои для Android. Как создать. Тонкости. Продвижение
 
Complete ruby code
Complete ruby codeComplete ruby code
Complete ruby code
 
Atlas an overview sept2012
Atlas   an overview sept2012Atlas   an overview sept2012
Atlas an overview sept2012
 
Atlas an overview sept2012
Atlas   an overview sept2012Atlas   an overview sept2012
Atlas an overview sept2012
 
Atlas - An Overview
Atlas - An OverviewAtlas - An Overview
Atlas - An Overview
 
Food security bill
Food security billFood security bill
Food security bill
 

Similar to Rails3 way

Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Ruby on Rails Security Updated (Rails 3) at RailsWayCon
Ruby on Rails Security Updated (Rails 3) at RailsWayConRuby on Rails Security Updated (Rails 3) at RailsWayCon
Ruby on Rails Security Updated (Rails 3) at RailsWayConheikowebers
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkBen Scofield
 
Deploy Rails Application by Capistrano
Deploy Rails Application by CapistranoDeploy Rails Application by Capistrano
Deploy Rails Application by CapistranoTasawr Interactive
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
GHC Participant Training
GHC Participant TrainingGHC Participant Training
GHC Participant TrainingAidIQ
 
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編Masakuni Kato
 
Integrating Flex And Rails With Ruby Amf
Integrating Flex And Rails With Ruby AmfIntegrating Flex And Rails With Ruby Amf
Integrating Flex And Rails With Ruby Amfrailsconf
 
Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails Mohit Jain
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascriptmpnkhan
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonManageIQ
 

Similar to Rails3 way (20)

Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Rails Page Caching
Rails Page CachingRails Page Caching
Rails Page Caching
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
 
Ruby on Rails Security Updated (Rails 3) at RailsWayCon
Ruby on Rails Security Updated (Rails 3) at RailsWayConRuby on Rails Security Updated (Rails 3) at RailsWayCon
Ruby on Rails Security Updated (Rails 3) at RailsWayCon
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Deploy Rails Application by Capistrano
Deploy Rails Application by CapistranoDeploy Rails Application by Capistrano
Deploy Rails Application by Capistrano
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
GHC Participant Training
GHC Participant TrainingGHC Participant Training
GHC Participant Training
 
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
 
Integrating Flex And Rails With Ruby Amf
Integrating Flex And Rails With Ruby AmfIntegrating Flex And Rails With Ruby Amf
Integrating Flex And Rails With Ruby Amf
 
Flex With Rubyamf
Flex With RubyamfFlex With Rubyamf
Flex With Rubyamf
 
Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascript
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
 

Rails3 way