ruby - Understanding rails delegate method -


i'm trying learn delegate method rails provides out of box. here i'm trying do. so, have accounts have_many tasks. i'm trying task count accounts , here how i'm doing it:

def total_tasks   tasks.count end 

pretty standard thing. i'm trying move method delegate method. i've tried it's not working

 delegate :count, to: :task, prefix: "total" 

that didn't work , didn't expect do. there way can this?

delegate :count, to: :tasks, prefix: "total" 

this meta programming creates method:

def total_count   tasks.send(:count) end 

this not fit delegate though should using size instead of count latter causes db query if association has been eager loaded.

def tasks_total   tasks.size # prevents n+1 query issue. end 

why want create method beyond me though 1 more character type.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -