ruby on rails - Completed Active Record -


i recieved task:

add method user model class called completed count, which:

• accepts user parameter

• determines number of todoitems user has completed using aggregate query function

– (hint: looking count of todoitems associated specific user completed:true)

• returns count

 class user < activerecord::base       has_one :profile, dependent: :destroy      has_many :todo_lists, dependent: :destroy      has_many :todo_items, through: :todo_lists, source: :todo_items, dependent: :destroy      validates :username, presence: true       def get_completed_count         todo_items.length     end    end 

does can explain complete method does?

thanks, michael.

so wrote "accepts user parameter" should following:

def self.get_completed_count(user)   user.todo_items.where(completed: true).count end 

and can call it:

user.get_completed_count(user) 

but above code doesn't make sense because better instance method:

def get_completed_count   self.todo_items.where(completed: true).count end 

this code return same results on instance.

you can call it:

user.find(id).get_completed_count 

i assume todoitem has completed boolean(better create scope , use scope inside method instead of where(completed: true)).


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -