Accessing attributes of one table from different model (which is related to another table)

Hello all, I referred the rails guides from documentation section, but I
cannot solve this problem.

Problem:

  1. I have member and receipt model (which should have corresponding
    members
    and receipts table).
  2. Now I created a model called recovery to write some business logic.
    (logic must be in models right?)
  3. And I have not created any association between any of the models.

What I want to do:

  1. I just want to retrieve all the member_code from members table whose
    ‘status’ is “active” from within recovery model.
  2. Once all member_code are available I want to compare those
    member_code
    with the member_code of receipts table. If those member_code exists in
    receipts table, I’ll run some business logic only on those members.

So all I want to do is access the attributes of members and
receipts
table from recovery model and I have created a button “start recovery
process” in view.html.erb of recoveries. I would like to execute the
business logic written inside recovery model once I press this button.

How can I point the button to the code inside model? Because by default
it
will pointing to actions inside controller right?

Can I do this without creating associations? Or is it compulsory?

Please help me

On 19 April 2015 at 08:14, Padmahas Bn [email protected] wrote:

Hello all, I referred the rails guides from documentation section, but I
cannot solve this problem.

Problem:

  1. I have member and receipt model (which should have corresponding members
    and receipts table).
  2. Now I created a model called recovery to write some business logic.
    (logic must be in models right?)

It depends on the logic

  1. And I have not created any association between any of the models.

Why not? Are they not related?

What I want to do:

  1. I just want to retrieve all the member_code from members table whose
    ‘status’ is “active” from within recovery model.

Member.where(status: “active”)
will give you the members, then you can use each to get
member.member_code

  1. Once all member_code are available I want to compare those member_code
    with the member_code of receipts table. If those member_code exists in
    receipts table, I’ll run some business logic only on those members.

Just compare them, what is the problem. However if you have the same
information in two models it is very likely that you database design
is poor. What is a member_code?

So all I want to do is access the attributes of members and receipts table
from recovery model and I have created a button “start recovery process” in
view.html.erb of recoveries. I would like to execute the business logic
written inside recovery model once I press this button.
How can I point the button to the code inside model? Because by default it
will pointing to actions inside controller right?

I think maybe you are missing some of the fundamentals of rails. I
suggest that before doing anything else you work right through a good
tutorial such as railstutorial.org (which is free to use online),
including all the exercises. That will show you the basics of rails.

Can I do this without creating associations? Or is it compulsory?

Nothing is compulsory.

Colin

On Sunday, April 19, 2015 at 2:25:54 PM UTC+5:30, Padmahas Bn wrote:

business logic written inside recovery model once I press this button.*
How can I point the button to the code inside model? Because by default it
will pointing to actions inside controller right?

Can I do this without creating associations? Or is it compulsory?

Please help me

Hello Collin Law,
Thank you for replying. To make clear why I want to do above things,
here
are the details. And yes I started with railstutorials.org and I was
able
to create successful login page etc. But this is the logic part I’m
trying
to implement. Here is the details.

  1. If I generate members model, then members table will be created in
    the
    database

  2. If I generate recovery model, then recovery
    “-----------------”---------------------------".

  3. And there is no association or any type of relation(one to many,
    has-many, belongs-to) between these two tables.

Now is it possible to access data of members table such as member_code,
member_status (active or inactive) etc from recovery model?

If possible how?

  1. I have members model and the table with so many attributes but right
    now
    I’m interested in member_code and member_status.

  2. I have loan model and the table. Attributes I’m interested in are
    loan_type (joint loan or personal loan).

  3. I have receipts table (Which contains the information of how much of
    the
    loan are already cleared by all members).

  4. Now this is the special case. This model called recovery, I created
    it
    just because I read that business logic should be implemented inside the
    model and this model will calculate the remaining amount to be paid by
    each
    members.

May be members, loan and receipts table have association or relation
between them, but in this recovery model all I want to do is just to
read
data from different tables and process the data and finally store them
inside recovery table.

How can I achieve this ?

Hello Collin Law,
Thank you for replying. To make clear why I want to do above things,
here
are the details. And yes I started with railstutorials.org and I was
able
to create successful login page etc. But this is the logic part I’m
trying
to implement. Here is the details.

  1. If I generate members model, then members table will be created in
    the
    database

  2. If I generate recovery model, then recovery
    “-----------------”---------------------------".

  3. And there is no association or any type of relation(one to many,
    has-many, belongs-to) between these two tables.

Now is it possible to access data of members table such as member_code,
member_status (active or inactive) etc from recovery model?

If possible how?

  1. I have members model and the table with so many attributes but right
    now
    I’m interested in member_code and member_status.

  2. I have loan model and the table. Attributes I’m interested in are
    loan_type (joint loan or personal loan).

  3. I have receipts table (Which contains the information of how much of
    the
    loan are already cleared by all members).

  4. Now this is the special case. This model called recovery, I created
    it
    just because I read that business logic should be implemented inside the
    model and this model will calculate the remaining amount to be paid by
    each
    members.

May be members, loan and receipts table have association or relation
between them, but in this recovery model all I want to do is just to
read
data from different tables and process the data and finally store them
inside recovery table.

How can I achieve this ?

On 20 April 2015 at 13:40, Padmahas Bn [email protected] wrote:

When replying please reply to the previous message, rather than your own
post.

Now is it possible to access data of members table such as member_code,
member_status (active or inactive) etc from recovery model?

I told you how to do that in my first reply, something like
Member.where(status: “active”)
will give you the members, then you can use each to get
member.member_code

Colin

Member.where(status: “active”)
will give you the members, then you can use each to get
member.member_code

Hello Colin thank you very much. I was able to access member and loan
table from recovery model.
In rails guides i’m not able to find how to pass a variable to where
clause.

eg: I have retrieved the member_code of all members whose status is
1(active) and extracted member_code like this

 @member = Member.where(record_status: "1")
   @member.each do |member|
         tmember_code = member.member_code

Now I want to pass tmember_code to where clause to perform some
operation
on LoanTable. Is this the correct way?

@loan = LoanTable.where(:memberCode => tmember_code, :loanType => “1”)

And I hope comma in this query will act as logical AND.

@loan = LoanTable.where(:memberCode => tmember_code, :loanType => “1”)

memberCode of LoanTable contains the same values of member_code of
Member
table.

On 23 April 2015 at 04:47, Padmahas Bn [email protected] wrote:

 @member = Member.where(record_status: "1")

That should be @members as it is plural

   @member.each do |member|
         tmember_code = member.member_code

Now I want to pass tmember_code to where clause to perform some operation on
LoanTable. Is this the correct way?

@loan = LoanTable.where(:memberCode => tmember_code, :loanType => “1”)

Best to stick to rails naming conventions, so it should be loan_type.
Assuming that member has_many loans then this would be
@loans = member.loans.where( loan_type: “1”)
If for some reason there is no such association (which seems very
unlikely) then section 2.2 of
Active Record Query Interface — Ruby on Rails Guides is probably
the way to go.

But as I said in an earlier mail the fact that you have member_code in
multiple tables is almost certainly an indication that your database
design is poor. You should be using relationships instead. It will
make your life much easier.

Colin

view.html.erb

<%=button_to( “start recovery process”, new_editor_path, :method => :get
)%>

class WelcomeController < ApplicationController
def index
@recovery=Recovery.new()
end
end

class Recovery < ActiveRecord::Base
attr_accessible :code, :name

@active_members=Member.where(status: ‘active’)
@active_members.each do |f|
@active_members=Receipt.find(f.id)
if @active_members.blank? == false
puts"—write your business logic—"
end
end

end

Hello Jeena, Thank you very much … This is exactly what I was looking
for. But I think I have to rename “def index” to “def new” in the
controller.

Hello Collin,

Changed the naming according to conventions of rails. And yeah about
database, I haven’t designed them with any relations yet.