Adding parameter to a scope

前端 未结 5 1614
长情又很酷
长情又很酷 2021-02-03 23:38

I have a ActiveRecord query for example like this:

@result = stuff.limit(10)

where stuff is a active record query with where clauses, order by,

相关标签:
5条回答
  • 2021-02-04 00:13

    The scope would look like any other (although you may prefer a class method), e.g.,

    class Stuff < ActiveRecord::Base
      def self.lim
        limit(3)
      end
    end
    
    > Stuff.lim.all
    => [#<Stuff id: 1, name: "foo", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
     #<Stuff id: 2, name: "bnar", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
     #<Stuff id: 3, name: "baz", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">]
    > Stuff.all.length
    => 8
    

    If you always (or "almost" always) want that limit, use a default scope:

    class Stuff < ActiveRecord::Base
      attr_accessible :name, :hdfs_file
    
      default_scope limit(3)
    end
    
    > Stuff.all
    => [#<Stuff id: 1, name: "foo", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
     #<Stuff id: 2, name: "bnar", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
     #<Stuff id: 3, name: "baz", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">]
    > Stuff.all.length
    => 3
    

    To skip the default scope:

    > Stuff.unscoped.all.size
    => 8
    
    0 讨论(0)
  • 2021-02-04 00:23

    Scope in Rails model with parameter:

    scope :scope_name, -> (parameter, ...) { where(is_deleted: parameter, ...) }  
    

    Or:

    scope :scope_name, lambda{|parameter, ...| where(is_deleted:parameter, ...)} 
    
    0 讨论(0)
  • 2021-02-04 00:24

    Pass parameters in Rails scope

    Definition of scope

    scope :name_of_scope, ->(parameter_name) {condition whatever you want to put in scope}
    

    Calling Method

    name_of_scope(parameter_name)
    
    0 讨论(0)
  • 2021-02-04 00:26

    There are indeed multiple ways of doing such, class methods are one as pointed out by @Dave Newton. If you'd like to use scopes, here's how:

    scope :max_records, lambda { |record_limit|
      limit(record_limit)
    }
    

    Or with the Ruby 1.9 "stabby" lambda syntax and multiple arguments:

    scope :max_records, ->(record_limit, foo_name) {   # No space between "->" and "("
      where(:foo => foo_name).limit(record_limit)
    }
    

    If you'd like to know the deeper differences between scopes and class methods, check out this blog post.

    Hope it helps. Cheers!

    0 讨论(0)
  • 2021-02-04 00:26

    Well Scopes are meant for this

    Scoping allows you to specify commonly-used Arel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as where, joins and includes. All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.

    Source: http://guides.rubyonrails.org/active_record_querying.html#scopes

    So if you feel that there are some common queries which you have, or you need some kind of chaining in your queries which are common to many. Then i will suggest you to go for scopes to prevent repetition.

    Now to answer how the scope will look like in your case

    class YourModel < ActiveRecord::Base
      scope :my_limit, ->(num) { limit(num)} 
      scope :your_where_condition, ->(num) { where("age > 10").mylimit(num) } 
    end
    
    0 讨论(0)
提交回复
热议问题