# Looking for ideas from a new ROR programmer

**URL:** <https://www.ruby-forum.com/t/looking-for-ideas-from-a-new-ror-programmer/104137>\
**Category:** Rails\
**Created:** [May 14, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/looking-for-ideas-from-a-new-ror-programmer/104137 "2007-05-14T18:02:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![freewolf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@freewolf](https://www.ruby-forum.com/u/freewolf)\
**Post date:** [May 14, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/looking-for-ideas-from-a-new-ror-programmer/104137/1 "2007-05-14T18:02:46Z")

</div>

Greetings all,

I am just getting into Ruby on Rails. I come from a LAMP background, and  
am throughly enjoying the ease of ROR.

I have set myself up to learn this through giving myself a small  
project. Learn through experience which is the best way that I learn. I  
am creating a small little accounting piece of software for myself.

So I have couple of questions I am hoping to get some ideas on how to  
handle stuff in ROR that I am familiar with in PHP but not quite so in  
this new environment.

So I have one set of scaffolding that handles what the debts that are  
owed are. I have another set of scaffold that handles what payments have  
been made.

What I want to do is to be able to tally up the payments that have been  
made to a particular debt and then display them under the debts listing  
to show what the new current total is.

So for example in the debts section:  
$100 To someone

I then list in the payments area two payments for $20 each.

So then in the debts section I would like to now see  
$60 To someone

I gather that whatever code I would need would go in the model?? But not  
sure what it would look like.

Secondly I was wondering how I would generate a global variable in ROR.

I have scaffolding for a set of pay periods. I want to be able to set  
one as the current period. Now when I go into the payments section, I  
would like to have the current pay period display in a drop down list.

I am figuring having that pay period that is current be set like a  
global variable then reading it in the form scaffolding for the  
payments. But I am thinking there is a different way of doing this in  
ROR. Just looking for some guidance here.

Thanks all.

Phillip

---

<div class="post-metadata">

**Author:** ![freewolf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@freewolf](https://www.ruby-forum.com/u/freewolf)\
**Post date:** [May 14, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/looking-for-ideas-from-a-new-ror-programmer/104137/2 "2007-05-14T18:39:31Z")

</div>

Welcome to Rails!

Your first question seems first like a modeling question. It seems to  
me you want two tables. Debts and Payments. In your debt model, you  
would declare has\_many :payments, and in the payment model you declare  
belongs\_to :debt. This implies that the payments table has debt\_id  
field that associates each payment to a single debt. Of course if  
payments can be applied to multiple debts then this breaks down and  
you would need to use a third table.

The key bit here is to learn how ActiveRecord Associations work and  
what they provide for you. For instance, with that structure you  
could do Debt.find(:all, :include =\> :payments) and it would  
automatically LEFT JOIN the payments and return the debts and all  
their payment objects in one query. However if you only need the  
total, it would be more efficient to set up a specific query using  
SUM() and GROUP BY clauses to return just the data you need.  
ActiveRecord supports this by allowing you to pass in the contents of  
each clause in the find options (select, joins, conditions, order,  
group, limit and offset).

As to your second question, what you are referring to as a global  
variable sounds more like a session variable (both in PHP and Rails).  
Your controller should initialize the session variable to its  
default. The menu would be set up as a select menu in the view.  
However, if you are re-using this menu in multiple views than I  
suggest making it into a helper. If you are using declarative  
scaffolding (ie. scaffold :model\_name in your controller) then you  
aren’t going to be able to edit anything. Instead use the script/  
generate scaffold … command from a terminal to generate the code so  
you can modify it. One thing to keep in mind is that controllers and  
views are tightly coupled–it’s more of a judgement call where to put  
things. Models, on the other hand, should be completely decoupled.  
That is, you should be able to use all your model code directly from  
the console (script/console) without any need for the rest of the  
Rails environment.

I realize there are a lot of pieces to figure out here, but stick with  
it. The key to the Rails way is knowing the purpose of each component  
and where code best fits. Past experience developing web applications  
will help because you understand what needs to be done behind the  
scenes.

On May 14, 10:02 am, Phillip B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
