Newbie Problems with pluralization


#1

Hi,

I have a many to many relationship incorporating the following tables:

taxes --> taxes_tax_groups --> tax_groups

I have a model named tax with the following declaration:

has_and_belongs_to_many :tax_groups

and I have a model named tax_group with the following declaration:

has_and_belongs_to_many :taxes

I am attempting to run the following unit test:

require File.dirname(FILE) + ‘/…/test_helper’

class TaxGroupTest < Test::Unit::TestCase
fixtures :taxes, :tax_groups, :taxes_tax_groups

Replace this with your real tests.

def setup
@taxgroup = TaxGroup.find(1)
end

Replace this with your real tests.

def test_create
assert_equal 1, @taxgroup.id
assert_equal “Ontario”, @taxgroup.name
assert_equal “Ontario w/ pst”, @taxgroup.description
assert_equal “GST”, @taxgroup.taxes[1].name
end
end

in which i get the following error:

  1. Error:
    test_create(TaxGroupTest):
    ActiveRecord::StatementInvalid: Mysql::Error: #42S02Table
    ‘imbibitive_test.taxes
    _group’ doesn’t exist: SELECT * FROM taxes_group WHERE (taxes_group.id =
  2. LIM
    IT 1

    1 tests, 0 assertions, 0 failures, 1 errors

Should it not pluralize the last work? Or am i missing something here?

Thanks!


#2

Hi,

I think, by default, the joint table should be called tax_groups_taxes.
The joint table name is alphabetical and the underscore in tax_groups
comes
before the ‘e’ in taxes.
At least that’s how I got it to work.

Cheers,
Andy


#3

Thanks for the help!

This now brings up another issue. Rails is pluralizing tax as taxis.

class TaxGroupTest < Test::Unit::TestCase
fixtures :taxes, :tax_groups, :tax_groups_taxes

Replace this with your real tests.

def setup
@taxgroup = TaxGroup.find(1)
end

Replace this with your real tests.

def test_create
assert_equal 1, @taxgroup.id
assert_equal “Ontario”, @taxgroup.name
assert_equal “Ontario w/ pst”, @taxgroup.description
assert_equal “GST”, @taxgroup.taxes[0].name
end
end

I complains on the @taxgroup.taxes[0].name line and says:
const_missing : uninitialized constant Taxis

Some how it is pluralizing taxes to taxis.

As suggested by another post on this mailing list I have placed this in
my
environment.rb:

Inflector.inflections do |inflect|
inflect.plural ‘tax’, ‘taxes’
inflect.singular ‘taxes’, ‘tax’

end

When i do that i get an sql error because it changes the tax_groups
table name
in the search to taxes_group.

test_create(TaxGroupTest):
ActiveRecord::StatementInvalid: Mysql::Error: #42S02Table
‘imbibitive_test.taxes
_group’ doesn’t exist: SELECT * FROM taxes_group WHERE (taxes_group.id =

  1. LIM
    IT 1

I’m thinking of turning off the pluralization all together, unless I can
find a
fix to this.

Thanks Again!


#4

if you start an interactive session using “ruby script\console” you’ll
find out that ruby correctly pluralizes “tax” to “taxes” without the
need to explicitly declare that.


#5

yeh as above, pluralisation works fine so you don’t need to modify
environment.rb.

passing class_name like this:
has_and_belongs_to_many :taxes, :class_name => ‘Tax’
seems to fix it. don’t have time to look into what’s the cause of the
original problem though.

As suggested by another post on this mailing list I have placed this in
my


#6

Firstly I didn’t have the problem when I put all the assert tests in
tax_test.rb but when I tried it again in tax_group_test.rb I got the
same
error as you.

I fixed it by passing :class_name into the habtm declaration in
tax_group