Reputation: 153
In my Rails application, I have a table where I keep my client invoices , and I want the system to auto generate an invoice to all clients once in every month automatically. Table clients
and table invoice
are associated to each other.
has_many :invoices
belongs_to :client
does anyone have any idea about how to do this automatically ?
The data must be generated on the first day of every month and must be kept in the invoices tables So, as you'ved guessed i'm really new to RoR , i'ved written a method in the invoices model to use in cron , can anyone look over it and tell if i'm on the correct path(i'm already using the app and don't want to mess the database), the value of the invoice (suma) is kept in clientpf.suma and my invoice model is named Factpf here is my method
def self.generare
@clientpfs = Clientpf.all
@clientpfs.each do |clientpf|
self.suma = clientpf.ab
self.clientpf_id = clientpf.id
Factpf.create!
end
end
Upvotes: 1
Views: 289
Reputation: 6918
Use Whenever gem and do as follows:
Example schedule.rb
file,
every '0 0 1 * *' do
runner "Model.your_method_generating_invoice"
end
The notation '0 0 1 * *' indicates that it will run once in every month.
To know more about the notation '0 0 1 * *', just refer here
Hope it helps :)
Upvotes: 1