简体   繁体   中英

How to call model method anywhere inside project without controller call?

I have model tasks inside of method name is not_completed_list have some records stored in @list variable .i need to loop that variable (@list)from view without controller how is it?

Task.rb

class Task < ApplicationRecord
    def self.not_completed_list
      @list= Person.pluck(:completed_on)#[name1,name2,name3]
    end
end

My view

not_completed_list.@list do |x|
  puts x#1sttime looping name1,2nd time..name2,3rd time name3
end

Just do following in view,

<% Task.not_completed_list.each do |completed_on| %>

And model should be,

class Task < ApplicationRecord
  def self.not_completed_list
    Person.pluck(:completed_on)
  end
end

but your implementation and flow need lots of changes.

You could get it your view simply without creating any class method,

<% Person.pluck(:completed_on).each do |completed_on| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM