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

33 views
Skip to first unread message

Padmahas Bn

unread,
Apr 19, 2015, 4:55:54 AM4/19/15
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Apr 19, 2015, 3:58:45 PM4/19/15
to rubyonra...@googlegroups.com
On 19 April 2015 at 08:14, Padmahas Bn <padm...@gmail.com> 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

> 3. 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

> 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.

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
Message has been deleted

Padmahas Bn

unread,
Apr 20, 2015, 8:46:12 AM4/20/15
to rubyonra...@googlegroups.com
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 ?

Colin Law

unread,
Apr 20, 2015, 9:18:33 AM4/20/15
to rubyonra...@googlegroups.com
On 20 April 2015 at 13:40, Padmahas Bn <padm...@gmail.com> 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

Padmahas Bn

unread,
Apr 22, 2015, 11:47:53 PM4/22/15
to rubyonra...@googlegroups.com
>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")

Padmahas Bn

unread,
Apr 22, 2015, 11:53:29 PM4/22/15
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Apr 23, 2015, 3:44:29 AM4/23/15
to rubyonra...@googlegroups.com
On 23 April 2015 at 04:47, Padmahas Bn <padm...@gmail.com> wrote:
>> >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")

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
http://guides.rubyonrails.org/active_record_querying.html 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

>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/460db5c7-6d46-4ba4-b713-a0a3a572d5bd%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

JEENA JACOB

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

Padmahas Bn

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Padmahas Bn

unread,
Apr 27, 2015, 4:27:36 AM4/27/15
to rubyonra...@googlegroups.com
Hello Collin,

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

Reply all
Reply to author
Forward
0 new messages