After processing data inside model,how to insert processed data(interest_to_be_paid etc) to database

class Recovery < ActiveRecord::Base

def self.processrecovery

@members = Member.where(record_status: “1”)

@members.each do |member|

tmemberCode = member.member_code

:

:

:# Processing of data from different database

:

:

# Variables containing processed data

interest_to_be_paid

pInstallmentAmt = tPersonalLoanBalAmt * tpersonalintrate / (100 * 12)

jInstallmentAmt = tJointLoanBalAmt * tjointintrate / (100 * 12)

end

end

Now how can I insert these processed data into recoveries table?

Should I user

Recovery.create(interest: => interest_to_be_paid, p_installement: => pInstallmentAmt,…)

inside recovery.rb (model) itself?

Why not? Presumably inside processrecovery.

Colin

Why not? Presumably inside processrecovery.

I didn’t get you. Do you mean I can use Recovery.create(…) directly inside processrecovery without sending them to controller?

If so how can I use params? Because I suspect Rails 4 won’t let me to directly insert data to tables without using strong parameters by permitting them.

Thank you.

Why not? Presumably inside processrecovery.

I didn't get you. Do you mean I can use Recovery.create(...) directly inside
processrecovery without sending them to controller?

Of course, see section 5 of
http://guides.rubyonrails.org/active_record_basics.html

If so how can I use params? Because I suspect Rails 4 won't let me to
directly insert data to tables without using strong parameters by permitting
them.

In Rails 4 the protection has been moved out of the model and into the
controller. You can do anything you like while manipulating models
directly rather than through controllers.

Colin

Of course, see section 5 of

http://guides.rubyonrails.org/active_record_basics.html

In Rails 4 the protection has been moved out of the model and into the

controller. You can do anything you like while manipulating models

directly rather than through controllers.

Sound like good new for me even though it is not recommended.

Anyway Thank you Collin

What is it you believe is "not recommended"?

What is it you believe is “not recommended”?

I mean that I’m not using association between tables when it is possible to easily cascade changes done in one table, And I’ve not seen anywhere inserting records through model directly and it’s not even showed as example in rails guides.

So I think the way of coding that I’m doing has to be improved and code has to be optimized.

I mean that I'm not using association between tables when it is possible to
easily cascade changes done in one table,

I'm afraid I don't understand that sentence,

And I've not seen anywhere inserting records through model directly

?? From your original example:

class Recovery < ActiveRecord::Base
  def self.processrecovery
    # do whatever
    create(interest: interest_to_be_paid, p_installment: pInstallmentAmt)
  end
end

And there you go, you've created a new Recovery object and saved
it to the database.

Try thinking less in terms of "tables" and more in terms of "objects"
and let ActiveRecord do its job as ORM :slight_smile:

FWIW,