Forum: Ruby on Rails Using fixtures with a legacy database

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
gael.pourriel (Guest)
on 2005-12-10 16:58
(Received via mailing list)
Hi all, I'm new to Rails, I got the book and I've got a question

I need to build a Rail application on top on an existing Database (MS
server) and the naming convention for the tables name is not quite the
as Rails.
The tables names aren't pluralize and they use capital letter at the
beginning of each word instead of using "_" to separate the words.
ClientPrice instead of client_price)

I've managed to create a model, thanks to Rails ability to override the
default settings, however when I come to test it using fixtures I'm
into troubles.

I've got a table called "ClientPrice", I've therefore created a model
"ClientPrice" and adjusted the table name and primary key name in the

class ClientPrice < ActiveRecord::Base
    set_table_name "ClientPrice"
    set_primary_key "ClientPrice_id"

This works fine and I can CRUD into this model using scaffolding.  Rails
also created a fixture file for me called "client_price". However I had
rename it to "clientprice" so it can load the data into the Test
Here's my sample fixture file:

  ClientPrice_id: 1
  Price: 1.5
  Unit: Dollars

Here's my problem, I wrote the following test file:

class ClientPriceTest < Test::Unit::TestCase
  fixtures :clientprice

  def test_model
      assert_equal clientprice(:price_for_client_A).Price,

And the assertion failed saying "NoMethodError: You have a nil object
you didn't expect it!"
It means that clientprice(:price_for_client_A) is retuning a NIL object
while it should return the 1st object of my fixture, to make the test to
work I have to do this:

assert_equal 1.5, ClientPrice.find(:first).Price

In this case it works because the database is correctly populated with

However if my table name is just called "Client" and my fixture file
"client.yml" then the above test works fine. Providing that my Model is
called "Client" and my test class called "ClientTest"

Any idea why that is?

matthew clark (Guest)
on 2006-02-21 21:51
(Received via mailing list)
Has anyone come up with a good solution to get around the set_table_name
fixtures issue?
This topic is locked and can not be replied to.