Rails 3 displaying tasks from partials
My Tasks belongs to different models but are always assigned to a company and/or a user. I am try开发者_开发百科ing to narrow what gets displayed by grouping them by there due_at date without doing to many queries.
Have a application helper
def current_tasks
if user_signed_in? && !current_company.blank?
@tasks = Task.where("assigned_company = ? OR assigned_to = ?", current_company, current_user)
@current_tasks = @tasks
else
@current_tasks = nil
end
end
Then in my Main view I have
<%= render :partial => "common/tasks_show", :locals => { :tasks => current_tasks }%>
My problem is that in my task class I have what you see below. I have the same as a scope just named due_today
. when I try current_tasks.due_today
it works if I try current_tasks.select_due_today
I get a undefined method "select_due_tomorrow" for #<ActiveRecord::Relation:0x66a7ee8>
def select_due_today
self.to_a.select{|task|task.due_at < Time.now.midnight || !task.due_at.blank?}
end
If you want to call current_tasks.select_due_today
then it'll have to be a class method, something like this (translating your Ruby into SQL):
def self.select_due_today
select( 'due_at < ? OR due_at IS NOT NULL', Time.now.midnight )
end
Or, you could have pretty much the same thing as a scope - but put it in a lambda so that Time.now.midnight
is called when you call the scope, not when you define it.
[edited to switch IS NULL
to IS NOT NULL
- this mirrors the Ruby in the question, but makes no sense because it will negate the left of the OR
s meaning]
精彩评论