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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 464
    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:

    1.     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:

    1.     class Blog < ActiveRecord::Base
    2.       scope :approved, -> { where(approved: true) }
    3.     end
    4.  
    5.     ## It is similar to
    6.     class Blog < ActiveRecord::Base
    7.       def self.approved
    8.         where(approved: true)
    9.       end
    10.     end
    11.  
    12.     ## Scope will be called with the class as it is a class method
    13.     >> Blog.approved
    14.     #=> 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:

    1.     class Blog < ActiveRecord::Base
    2.       scope :approved, -> { where(approved: true) }
    3.      scope :video_content, -> { where(content_type: "video") }
    4.      scope :text_content, -> { where(content_type: "text") }
    5.     end
    6.  
    7.     >> Blog.approved
    8.     #=> It will return all the approved blogs
    9.  
    10.     >> Blog.approved.video_content
    11.     #=> It will return all the approved blogs with video content
    12.  
    13.     >> Blog.approved.text_content
    14.     #=> 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

    1.     class Blog < ActiveRecord::Base
    2.       default_scope :approved, -> { where(approved: true) }
    3.      scope :video_content, -> { where(content_type: "video") }
    4.      scope :text_content, -> { where(content_type: "text") }
    5.     end
    6.  
    7.     >> Blog.all
    8.     #=> It will return only the approved blogs
    9.  
    10.     >> Blog.video_content
    11.     #=> It will return all the approved blogs with video content
    12.  
    13.     >> Blog.text_content
    14.     #=> It will return all the approved blogs with text content
    15.  
    16.     ## For removing the default scope in case if you need it somewhere you can use unscoped
    17.     >> Blog.unscoped.load
    18.     #=> 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

    1.     class Blog < ActiveRecord::Base
    2.       default_scope :approved, -> { where(approved: true) }
    3.      scope :my_blogs(user), -> { where(user_id: user.id) }
    4.      scope :other_blogs(user), -> { where.not(user_id: user.id) }
    5.     end
    6.  
    7.     >> Blog.all
    8.     #=> It will return only the approved blogs
    9.  
    10.     >> Blog.my_blogs(current_user) # here current_user is logged in user's object
    11.     #=> It will return all of my approved blogs
    12.  
    13.     >> Blog.other_blogs(current_user) # here current_user is logged in user's object
    14.     #=> 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
Reset Password
Fill out the form below and reset your password: