I am creating a very simple rails 3 application to view data in an outdated MySQL database. The legacy database is mostly compatible with ORM rails, except that the foreign key fields are pluralized. For example, the orders table has a foreign key field in the company table called company_id (and not company_id). So naturally, I need to use the attribute:: foreign_key for my own_to to set the field name manually.
I have not used rails in a few years, but I am sure that I am doing everything correctly, but when I try to access "order.currency.code", the following error appears:
undefined method `code' for nil:NilClass
This is a very simple application. The only thing I did was generate an application and a bunch of forests for each of the old database tables. Then I went over to some models to make adjustments to match the above difference in the database naming conventions and added some fields to the views. It. No funny thing.
So my database tables look like this (only relevant fields):
orders
id
description
invoice_number
currencies_id
currencies
id
code
description
My order model is as follows:
class Order < ActiveRecord::Base
belongs_to :currency, :foreign_key=>'currencies_id'
end
My currency model is as follows:
class Currency < ActiveRecord::Base
has_many :orders
end
The corresponding fragment of the view is as follows:
<% @orders.each do |order| %>
<tr>
<td><%= order.description %></td>
<td><%= order.invoice_number %></td>
<td><%= order.currency.code %></td>
</tr>
<% end %>
I have a lot of ideas. Any suggestions?
source
share