Setting default_scope according to some criteria
Asked Answered
K

2

1

I'm trying to set default scope according to some criteria determined by ana ActionController before_filter. In controller:

  before_filter :authorize
  ...
  def authorize
    if some_condition
      @default_scope_conditions = something
    elsif another_condition
      @default_scope_conditions = something_else
    end
  end

Inside the ActiveRecord

default_scope :conditions => @default_scope_conditions

But it doesn't seem to work, the before filter gets called but the default_scope doesn't get set. Could you please advise me what I'm doing wrong and how to fix it or suggest me some other way of achieving that.

Konstanz answered 17/4, 2010 at 9:35 Comment(1)
You set @default_scope_conditions - which is an instance variable from the controller and you expect to read it from the model. It is not visible from the model unless passed as method parameter.Taxdeductible
T
1

You set @default_scope_conditions - which is an instance variable from the controller and you expect to read it from the model. It is not visible from the model unless passed as method parameter.

More, this approach would break the MVC principle that separates the model logic from the controller logic: Your model shouldn't automatically access info about current state of the controller.

What you can do: use anonymous scopes.

  def scope_user
    if some_condition
      @default_scope_conditions = something
    elsif another_condition
      @default_scope_conditions = something_else
    end

    @user_scoped = User.scoped(:conditions => @default_scope_conditions)
  end

Then, in your method, you can:

def my_method
  users = @user_scoped.all
end

or

def my_method
  User.with_scope(:conditions => @default_scope_conditions) do
    # .. 
    @users = User.all #users get scoped
    @products.users # also gets scoped
  end
end
Taxdeductible answered 17/4, 2010 at 10:23 Comment(0)
L
-1

Try one default_scope and override it using custome finder.

The default options can always be overridden using a custom finder.

class User < ActiveRecord::Base
  default_scope :order => '`users`.name asc'
end

User.all # will use default scope User.all(:order => 'name desc') # will use passed in order option.

Then you can try something like following

before_filter :authorize
  ...
  def authorize
    if some_condition
      @order = ''  # default scope
    elsif another_condition
      @order = something_else
    end
  end

  def method_name
    User.all(:order => @order)
  end

No Check though.

Lakin answered 17/4, 2010 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.