根据某些条件动态“named_scope”

发布于 2024-10-21 11:38:44 字数 546 浏览 4 评论 0原文

亲爱的大家,我有一个 Student 模型,我在其中指定了一些 name_scope,例如 from_programfrom_yearfrom_schoolhas_statusfrom_course等...

无论如何我都可以将不同的named_scope链接在一起在运行时动态地取决于某些标准?

例如,如果访问数据的用户来自 Finance,我希望能够仅将 from_schoolhas_status 链接在一起。如果用户是讲师,我希望能够将 from_coursefrom_school 链接在一起,依此类推...

我应该使用 named_scope ?或者我应该回到指定条件的好旧方式?

提前感谢您的建议! =)顺便说一句,我正在使用 Rails 2.3

dear all, i have a Student model that i've specified some name_scope in it, e.g. from_program, from_year, from_school, has_status, from_course, etc...

is there anyway that i can chain the different named_scope together dynamically depending on certain criterias during runtime?

for example, if the user accessing the data is from Finance, i want to be able to chain from_school and has_status together only. if the user is the lecturer, i want to be able to chain from_course, from_school together, and so on...

should i use named_scope? or should i just fall back to the good old way of specifying conditions?

thanks for your suggestions in advance! =) btw i'm using rails 2.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-10-28 11:38:44

我不确定我是否理解,但我认为你可以做这样的事情:

class Student

  named_scope from_program, lambda{|program| :conditions => {:program => program}}
  named_scope from_year, lambda{|year| :conditions => {:year => year}}
  named_scope has_status, lambda{|status| :conditions => {:status => status}}

  def self.from_finance(school, status)
    self.from_school(school).has_status(status)
  end

end

或更一般

def self.get_students(params)
  scope = self
  [:program, :year, :school, :course].each do |s|
    scope = scope.send("from_#{s}", params[s]) if params[s].present?
  end
  scope = scope.has_status(params[:status]) if params[:status].present?
  scope
end

I'm not sure, if I understood, but I think you could do something like this:

class Student

  named_scope from_program, lambda{|program| :conditions => {:program => program}}
  named_scope from_year, lambda{|year| :conditions => {:year => year}}
  named_scope has_status, lambda{|status| :conditions => {:status => status}}

  def self.from_finance(school, status)
    self.from_school(school).has_status(status)
  end

end

or more general

def self.get_students(params)
  scope = self
  [:program, :year, :school, :course].each do |s|
    scope = scope.send("from_#{s}", params[s]) if params[s].present?
  end
  scope = scope.has_status(params[:status]) if params[:status].present?
  scope
end
温柔嚣张 2024-10-28 11:38:44

你可以尝试这样的事情

  Class User extend ActiveRecord::Base
  belongs_to :semester

  named_scope :year, lambda { |*year|
    if year.empty? || year.first.nil?
      { :joins => :semester, :conditions => ["year = #{CURRENT_SEMESTER}"]}
    else
      { :joins => :semester, :conditions => ["year = #{year}"]}
    end
    }

  end

你可以像这样调用

  User.year     # defaults to CURRENT_SEMESTER constant
  User.year()  # same as above
  User.year(nil)  # same as above; useful if passing a param value that may or may not exist, ie, param[:year]
  User.year(2010)

以同样的方式你可以传递参数

You can try something like this

  Class User extend ActiveRecord::Base
  belongs_to :semester

  named_scope :year, lambda { |*year|
    if year.empty? || year.first.nil?
      { :joins => :semester, :conditions => ["year = #{CURRENT_SEMESTER}"]}
    else
      { :joins => :semester, :conditions => ["year = #{year}"]}
    end
    }

  end

You can call like this

  User.year     # defaults to CURRENT_SEMESTER constant
  User.year()  # same as above
  User.year(nil)  # same as above; useful if passing a param value that may or may not exist, ie, param[:year]
  User.year(2010)

In the same way you can pass parameters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文