Topic: Currency, and foreign keys

Hi All,

I'm still learning my way around Rails, & spent this evening creating my data models, & learning how to use migrations.  As a result of this I have a couple of questions, one pertaining to currency, the other to foreign keys. 

Hopefully someone here will have the time & patience to answer them :-)

Firstly, currency values - what type should I be using to store them?  I was thinking of using decimal, but it turns out that I can't do that, because migration doesn't support decimal:

-- add_column(:roles, :billing_period_cost, :decimal)
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.

So, I'm using integer at the moment (storing the number of cents), but that's not ideal, as I'll have to translate the user input (in the form "123.45") to cents (12345) at every point.

Secondly, foreign keys.  I have a Payment model, which contains a category_id column that identifies a Category.  In the models, I have:

class Payment < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  belongs_to :user
  has_many :payments
end


However, the UI created by Rails doesn't show the Category in the Payment.  If for example I navigate to /payment/new, I see all of my fields in the new Payment, except for category_id.

Am I missing something here?  Do I need to manually code the category integration in the views for new, show, list, etc.?  Or do I need to take some other step to notify Rails that there's a relationship between the two tables?

TIA for any advice.

Yours,
Duncan Bayne

Last edited by duncan_bayne (2006-12-11 05:45:45)

Re: Currency, and foreign keys

Rails supports decimals in 1.2, which will be out very soon (a week or two?). Once that's out, you could do it like this:

add_column :roles, :billing_period_cost, :decimal, :precision => 10, :scale => 2, :default => 0

Rails currently supports floating points, but that is subject to rounding errors, so cents is the best way to go until 1.2.

duncan_bayne wrote:

However, the UI created by Rails doesn't show the Category in the Payment.  If for example I navigate to /payment/new, I see all of my fields in the new Payment, except for category_id.

Am I missing something here?  Do I need to manually code the category integration in the views for new, show, list, etc.?  Or do I need to take some other step to notify Rails that there's a relationship between the two tables?

Scaffolding is very simple, and only meant to show you how to do basic CRUD operations on a single model. Rails expects you to extend scaffolding to suit your needs. There are many ways to handle the one-to-many relationship. One way is to have an "add payment" link under each category. In that case the category_id could be passed through to the payment's create action so the association is handled behind the scenes.

Another way is to use a select menu. You can easily do it like this in your payment's form partial:

<%= collection_select :payment, :category_id, Category.find(:all), :id, :name %>

See the collection_select docs for details.

Railscasts - Free Ruby on Rails Screencasts

Re: Currency, and foreign keys

ryanb wrote:

See the collection_select docs for details.

Thanks.  A lot of my initial work in Rails was guided by the "Rolling with Rails" tutorial ... which made no mention of the FormOptionsHelper.  I think that tutorial is a bit obsolete :-)

Re: Currency, and foreign keys

It's honestly hard to find a good beginner's tutorial that's not obsolete. I certainly hope we will see more with 1.2 around the corner. At least Agile Web Development with Rails 2nd Edition is almost out now!

Railscasts - Free Ruby on Rails Screencasts

Re: Currency, and foreign keys

I've actually just bought the PDF of Rails Recipes and am finding it quite the eye opener :-)  That, and this forum, are proving to be an incredibly valuable resource.