控制器中的调用方法模型 rails 4

Call Method Model in a Controller rails 4

newTransaction 不保存。

方法模型:

def self.newTransaction(sesion,date) 
   t = Transaccion.new(sesion:'sesion',date:'date')                         
   newTransaction.save
end

控制器:

Transaccion.newTransaction("vianny.mo@gmail.com","12-12-12")

使用 t.save 代替 newTransaction.save

为什么不直接使用 create 并节省额外的行

def self.newTransaction(sesion, date) 
  create(sesion: sesion, date: date)                         
end

因为方法 (create) 没有接收者,它会被发送给自己,我假设它是相同的 class 你想为

创建方法