Here are some useful tips, ideas and resources for both newbie and professional in Ruby on Rails technology.
1. Avoid scenarios where multiple methods have the same functionality and operate on different class object.
For example: email validation may work upon user table, customer table and contact table. Instead of writing the same code 3 times, programmer can define a single module on email validation and include it into the desired classes.
2. Use named scope as it helps to follow dry principle. Named scope is chainable so it can chain up many named scope.
For example: many times we wish to implement both searching as well as sorting functionality simultaneously. Here’s a class called “Contact”, which contains name and description field, which will facilitate concurrent searching and sorting by name and description.
class Contact < ActiveRecord::Base
attr_accessible :description, :name
scope :matching, lambda { |column, value| where("#{column} like ?", "%#{value}%") if value.present? }
scope :order_by,lambda { |column,order| order("#{column} #{order}") if order.blank? || ["asc","desc"].include?(order.downcase)}
end
Contact.matching("name",params[:name]).matching("description","").order_by(params[:sort_col umn],params[:sort_order])
3. Extract composed class from your model.
Suppose contact has address fields like address_city and address_street you can extract address
class Customer < ActiveRecord::Base
composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
end
class Address
attr_reader :street, :city
def initialize(street, city)
@street, @city = street, city
end
...
end
4.
Observer is observe the object and on certain event do some stuff like sending notification to project members after creating project
class ProjectObserver < ActiveRecord::Observer
observe Project
def after_create(project)
project.members.each do |member|
ProjectMailer.deliver_notice(project, member)
end
end
end
5. Use seed data for populating default data
6. Indexing: It helps to improve performance of queries. For instance: add_index :comments, :post_id
7. Never write your logic in Views. Move code to helper or use drapper or presenter instead.
8. Avoid instance variable when it's not required
9. Avoid N+1 queries using include and join depending upon need and also don't use eager loading when it's not required
10. Use counter cache to improve performance (Counting total no of tasks for project)
(Use bullet gem to identify N+1 queries and it helps to identify counter-cache-column)
11. Keep consistent structure in model like Constant, Association, Validation, Callback, Class method, instance method
12. Annotate your models using (annoted or annoted-rails gem)
13. Write comment on your code specially magic code(Metaprogramming)
14. Use memoization rails3.2.x uses default ruby memoization
@user||=User.create(...)
15. To operate large data set use find_each or find_in_batches
16. Use helper method for dynamic layout or setting dynamic class
17. For polymorphic association use polymorphic routes. For example: Article and forum both has comments as Commentable
polymorphic_path([object, Comment])#/posts/1/comments" or "forums/1/comments"
18. Use delegate to expose contained objects’ methods as your own
class Task
belongs_to :project
delegate :name, :to => :project
end
Task.first.project_name
19. Use debugger or pry to debug your application
Please feel free to share your tips, ideas and suggestions in the comments to this post.
Some Handy Tips for Ruby on Rails Developers