Rails 4 scope with multiple conditions -


i want show active users within 1 day.

the member model , scope:

time_range = (time.now - 1.day)..time.now scope :active, -> { where(created_at: time_range, gold_member: true, registered: true) } 

however, when call

@member = user.active 

the below error rendered:

nomethoderror: undefined method `call' #<user::activerecord_relation:0x07fe068> 

please advise.

the backtrace:

  /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/relation/delegation.rb:136:in `method_missing'   /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/relation/delegation.rb:99:in `method_missing'   /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/scoping/named.rb:151:in `block (2 levels) in scope'   /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/relation.rb:292:in `scoping'   /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/scoping/named.rb:151:in `block in scope'   (irb):48   /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.1.7/lib/rails/commands/console.rb:90:in `start'   /users/jason/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.1.7/lib/rails/commands/console.rb:9:in `start' 

it won't fix error, there's problem way define scope. before it, define local variable time_range. problem is, define in body of activerecord class, evaluated 1 time in production environment, when class loaded. should evaluate time range inside of lambda:

scope :active, -> {   where(created_at: (time.now - 1.day)..time.now, gold_member: true, registered: true) } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -