Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

rails scope syntax

class Test < ActiveRecord::Model
  scope :complicated, ComplicatedScope
	
  class ComplicatedScope
    def self.call(*args) # in new syntax use (...)
      new(*args).call
    end

    def initialize(arg_1) # provide scope arguments
      @arg_1 = arg_1
    end

    def call
      module_parent
        .joins("complicated JOIN querry")
        .where("complicated where ?", @arg_1)
        .merge("some merging")
    end

    delegate :module_parent, to: :class
  end
end

Test.complcated("arg_1")
Source by guides.rubyonrails.org #
 
PREVIOUS NEXT
Tagged: #rails #scope #syntax
ADD COMMENT
Topic
Name
1+2 =