Forum: Ruby on Rails Looking for ideas from a new ROR programmer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Phillip B. (Guest)
on 2007-05-14 20:02
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
dasil003 (Guest)
on 2007-05-14 20:39
(Received via mailing list)
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>
This topic is locked and can not be replied to.