Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rails Scopes

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 423
    Comment on it

    Rails: Scopes
    Rails is made for building applications rapidly. Scope is an another example that truly admires this feature. Its use can be explained by taking an example. Suppose you have a blogging site and you want to show everywhere only those posts that are being approved by admin and there are many places where the blogs are being listed. So for this every place you need to write this where condition:

    	blog = Blog.where(approved: true)
    

    Now this is not the smart way of doing this. For doing it smartly rails provides scopes. It works like a class method and returns an ActiveRecord Relation so that you can have further queries on it. It can be used like:

    	class Blog < ActiveRecord::Base
    	  scope :approved, -> { where(approved: true) }
    	end
    
    	## It is similar to
    	class Blog < ActiveRecord::Base
    	  def self.approved
    	    where(approved: true)
    	  end
    	end
    
    	## Scope will be called with the class as it is a class method
    	>> Blog.approved
    	#=> It will return all the approved blogs
    
    

    Chaining is also possible here so if you have two or multiple scopes you can simply add them as chaing and it will work similar to as where chaining works:

    	class Blog < ActiveRecord::Base
    	  scope :approved, -> { where(approved: true) }
    	  scope :video_content, -> { where(content_type: "video") }
    	  scope :text_content, -> { where(content_type: "text") }
    	end
    
    	>> Blog.approved
    	#=> It will return all the approved blogs
    
    	>> Blog.approved.video_content
    	#=> It will return all the approved blogs with video content
    
    	>> Blog.approved.text_content
    	#=> It will return all the approved blogs with text content
    

    You can apply a default scope to the class, then even if that scope is not called it will automatically be applied to the class everytime whenever that class is called

    	class Blog < ActiveRecord::Base
    	  default_scope :approved, -> { where(approved: true) }
    	  scope :video_content, -> { where(content_type: "video") }
    	  scope :text_content, -> { where(content_type: "text") }
    	end
    
    	>> Blog.all
    	#=> It will return only the approved blogs
    
    	>> Blog.video_content
    	#=> It will return all the approved blogs with video content
    
    	>> Blog.text_content
    	#=> It will return all the approved blogs with text content
    
    	## For removing the default scope in case if you need it somewhere you can use unscoped
    	>> Blog.unscoped.load
    	#=> It will return all the blogs and will not use the default scope
    

    We can also pass arguments to the scopes similar to methods according to requirements like

    	class Blog < ActiveRecord::Base
    	  default_scope :approved, -> { where(approved: true) }
    	  scope :my_blogs(user), -> { where(user_id: user.id) }
    	  scope :other_blogs(user), -> { where.not(user_id: user.id) }
    	end
    
    	>> Blog.all
    	#=> It will return only the approved blogs
    
    	>> Blog.my_blogs(current_user) # here current_user is logged in user's object
    	#=> It will return all of my approved blogs
    
    	>> Blog.other_blogs(current_user) # here current_user is logged in user's object
    	#=> It will return all approved blogs except mine
    


    Hope you enjoyed reading this blog. For more like this. Please go this Link.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: