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

17 views
Skip to first unread message

Padmahas Bn

unread,
Apr 27, 2015, 4:42:38 AM4/27/15
to rubyonra...@googlegroups.com
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?

Colin Law

unread,
Apr 27, 2015, 5:07:15 AM4/27/15
to rubyonra...@googlegroups.com
Why not? Presumably inside processrecovery.

Colin

Padmahas Bn

unread,
Apr 27, 2015, 7:23:03 AM4/27/15
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Apr 27, 2015, 8:37:29 AM4/27/15
to rubyonra...@googlegroups.com
On 27 April 2015 at 12:23, Padmahas Bn <padm...@gmail.com> wrote:
>> 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

Padmahas Bn

unread,
Apr 27, 2015, 9:04:45 AM4/27/15
to rubyonra...@googlegroups.com
>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 

Hassan Schroeder

unread,
Apr 27, 2015, 10:51:01 AM4/27/15
to rubyonrails-talk
On Mon, Apr 27, 2015 at 6:04 AM, Padmahas Bn <padm...@gmail.com> wrote:

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

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

What is it you believe is "not recommended"?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Padmahas Bn

unread,
Apr 29, 2015, 12:08:27 PM4/29/15
to rubyonra...@googlegroups.com
>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.

Hassan Schroeder

unread,
Apr 29, 2015, 4:15:55 PM4/29/15
to rubyonrails-talk
On Wed, Apr 29, 2015 at 9:08 AM, Padmahas Bn <padm...@gmail.com> wrote:

> 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 :-)

FWIW,
Reply all
Reply to author
Forward
0 new messages