开发者

rails3 is it possible to create a model.scope with no constraints

I am trying to create a scope on a model of mine limiting the available results to only those that are owned by the user's partner. When the user is an administrator,开发者_运维百科 however, I want all models to be available. This works, but looks stupid. What is the proper rails3 way of expressing this?

scope :accessible_by, proc { |user|
  if user.admin?
    where("1=1")
  else
    where(:owner_id => user.partner.id)
  end
}

What I want to be able to do is select further and do e.g

@models = MyModel.
            accessible_by(current_user).
            other_scope.
            where(:property => value).
            order("another_property desc").
          all


You might be able to use the all modifier.

scope :accessible_by, proc { |user|
  if user.admin? == false
    where(:owner_id => user.partner.id)
  end
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜