Ruby-on-rails – Dynamic `named_scope` depends on some standards

Dear, I have a student model, I have specified some name_scopes in it, such as from_program, from_year, from_school, has_status, from_course, etc…

Anyway, can I dynamically link different named_scopes together based on some criteria at runtime?

For example, if the user accessing the data comes from the finance department, I want to be able to link only from_school and has_status together. If the user is a lecturer, I want to be able to link from_course, from_school, etc…

Should I use named_scope? Or should I go back to the old way of specifying conditions?

Thank you in advance for your suggestions! =) By the way, I am using rails 2.3

I’m not sure if I understand, but I think you can do 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

Dear, I have A student model, I have specified some name_scopes in it, such as from_program, from_year, from_school, has_status, from_course, etc....

Anyway, I can use a Do these standards dynamically link different named_scopes together?

For example, if the user accessing the data comes from the finance department, I want to be able to link only from_school and has_status together. If the user is a lecturer, I want to be able to link from_course, from_school, etc...

Should I use named_scope? Or should I go back to the old way of specifying conditions?

Thank you in advance for your suggestions! =) By the way, I am using rails 2.3

I am not sure, if I understand, but I think you can do 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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3322 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.