ruby on rails - Minimizing queries - Sorting results from a model -


i have client has_many :tasks. want sort these tasks separate variables, or hash, available view depending on date due.

i'm trying minimize queries, , know @ point should pull tasks client, sort each task variable. can using controller method , before_filter when loading show action:

 def build_client_tasks     @tasks = client.tasks.due     @tasks_today = []     @tasks_tomorrow = []     @tasks_upcoming = []     @tasks_later = []     task in @tasks       if task.due_date <= date.today         @tasks_today << task       elsif task.due_date == date.tomorrow         @tasks_tomorrow << task       elsif task.due_date > date.tomorrow && task.due_date <= 7.days.from_now.to_date         @tasks_upcoming << task       else         @tasks_later << task       end     end   end 

is there better/smarter way this? works fine, if want reload these tasks when user adds new task via ajax? i'm forced duplicate code in *.js.erb file available when tasks partial reloaded.

there must better way. in advance help!

regarding comment: not create different database cells. should have 1 due_date. scope block this:

class task < activerecord::base

scope :due_dated, lambda {|date| where(:due_date => date) } 

end

you can this:

task.due_dated(date.today) 

hi steve, might want try define in tasks/client model.

class task < activerecord::base  named_scope :today, :conditions => { :due => date.today }  ...  end  class client < activerecord::base  def self.tasks_due_today     tasks.today end   end 

and in view this:

<%=  @client.tasks_due_today. each |ct| %> <li><%=  ct.content  %> <%end%> 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -