Tag Archives: some

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.

Learn some basics about Ruby on Rails Programming Language

Ruby approximately Rails is considered now because a momentous revolution inside the computer programming code. Quite people are not aware of the cat which ROR is the brief shape of Ruby approximately Rails. Considered because the lightening quick programming code accessible now, it’s very practical to discover the standard details regarding this software. 

Ruby approximately Rails was produced by David Heinemeier Hansson inside the year 2003. As approximately now, the core team of Ruby approximately rails consists of over 1500 contributors that are sparing a commendable function inside introducing tremendous qualities into this very popular programme.

Ruby approximately rails is very practical inside developing a url much quicker with wonderful ease. ROR is understood because a pretty absorbing framework which has found its implementation inside most the internet solutions evolved now.

Popular sites including Base Camp, Penny Arcade, Urban Dictionary, Crunch Base, Scribed, XING Github, Hulu, Spice functions, Twitter, plus Yellowpages.com have employed ruby approximately rails to innovate a quantity of astonishing attributes inside their sites.

There are a amount of factors which distinguish ruby approximately rails from additional programming languages including the following:

1. ROR will aid operating internet applications inside a limited days.
2. ROR is dynamic, commanding plus wistful
3. ROR is an object-oriented code for programming
4. ROR could at when assist several programming paradigms
5. ROR is a lot simpler to create than shell scripts
6. ROR qualities massive amount of plug-ins
7. It is effortless to develop a question with ROR

ROR could complete a internet programming inside a some days that might otherwise need months to complete utilizing additional programming languages. This really is as a result of the swift phased convenient development techniques plus consumer friendly qualities forming element of ROR.

You are able to consult twitter to update a knowledge regarding ROR. Additionally, rubyonrails.com, the official url of the programme may assist the starters gain a comfortable knowledge into the basic concepts of ROR.

Alisha North writes thoroughly regarding Ruby approximately Rails plus his articles are read by fanatics & moreover experts.

Ruby approximately Rails Tutorial Part 1 - Getting Started

In this Ruby approximately Rails Blog Tutorial we’ll function approximately getting a blogging application working, discover regarding the rails directory structure, plus perform a simple h…

More Ruby On Rails Articles