1. The document provides tips and tricks for optimizing and customizing Rails applications. It discusses loading gems, changing backtraces, logging, routing, Active Record queries and scopes, callbacks, validations, and more.
2. Key topics include setting gem load precedence, silencing noisy libraries in backtraces, logging to syslog, redirecting and collecting routes, listing controller routes, and avoiding member actions.
3. Other useful tips include reading attributes before type casting, selecting attributes with calculated values, using Time instead of DateTime, reloading associations, and defining callback classes.
9. List of controller routes
$ rake routes CONTROLLER=products
Will show the list of ProductsController routes
10. 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¡¯
21. Difference between << and
create on the associations
<< is transactional, but create is not
<< triggers :before_add and :after_add callbacks but create
doesn¡¯t
22. Method ¡®clear¡¯ on the
associations
Transactionally removes all records and triggers callbacks
25. :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
27. validates on
class Report < AR
validates_presence_of :name, :on => :publish
end
report.valid? :publish
Validators for specific fields
28. Merging scopes
scope :tardy, :lambda {
joins(:timesheets).group(¡°users.id¡±) & Timesheet.late
}
For this purpose you are able to use merge method too
29. 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
30. 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