Friday, February 28, 2014

Some Handy Tips for Ruby on Rails Developers

 

Here are several beneficial strategies, inspirations plus resources for both newbie plus expert inside Ruby approximately Rails technology.

1. Avoid scenarios where numerous techniques have the same functionality plus work approximately different class object.

For example: e-mail validation could function on consumer table, customer table plus contact table. Instead of composing the same code 3 occasions, programmer will define a single component approximately e-mail validation plus include it into the desired classes.

2. Use called scope because it helps you to follow dry principle. Named range is chainable thus it will chain up various called range.

For example: numerous occasions you want to apply both searching & sorting functionality simultaneously. Here’s a class called “Contact”, that contains name plus description field, that will facilitate concurrent searching plus sorting by name plus description.

class Contact
  attr_accessible :description, :name
range :matching, lambda { |column, value|  where(“#{column} like ?”, “%#{value}%”) if value.present? }
range :order_by,lambda { |column,order| order(“#{column} #{order|purchase}”) 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 the model.

Suppose contact has address fields like address_city plus address_street you are able to extract address

class Customer
 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:

Observer is observe the object plus approximately certain event do several stuff like sending notice to  project members following creating project

class ProjectObserver
 observe Project
 def after_create(project)
project.members.each do |member|
  ProjectMailer.deliver_notice(project, member)
end
 end
end

5. Utilize seed information for populating standard data

6.   Indexing: It helps you to better performance of questions. For instance: add_index :comments, :post_id

7. Never write a logic inside Views. Move code to helper or employ drapper or presenter rather.

8. Avoid example variable whenever it’s not required

9.  Avoid N+1 queries utilizing include plus join depending on need & don’t employ eager loading whenever it’s not required

10.  Utilize counter cache to better performance (Counting total no of jobs for project)

(Use bullet gem to find N+1 questions plus it helps you to identify counter-cache-column)

11.  Keep consistent structure inside model like Constant, Association, Validation, Callback, Class way, example method

12.  Annotate the models utilizing (annoted or annoted-rails gem)

13.  Write comment approximately the code specifically magic code(Metaprogramming)

14.  Utilize memoization rails3.2.x utilizes standard ruby memoization

  @user||=User.create(…)

15.  To work huge information set utilize find_each or  find_in_batches

16.  Utilize helper method for dynamic design or setting dynamic class

17.  For polymorphic association employ polymorphic routes. For example: Article plus forum both has comments because Commentable

polymorphic_path([object, Comment])#/posts/1/comments” or “forums/1/comments”

18. Utilize delegate to expose contained objects’ techniques because a own

  class Task

  belongs_to :project
delegate :name,  :to => :project
  end
  Task.first.project_name

19.  Utilize debugger or pry to debug a application

 

Please feel free to share a tricks, inspirations plus suggestions inside the comments to the post.
 

Sherri Jones is a Web-Developer plus Ruby Programmer. The cause of composing the above mentioned post is to offer with certain significant Tips approximately Ruby approximately Rails for Developers.

No comments:

Post a Comment