Forum: Ruby on Rails Newbie Problems with pluralization

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.
3b87f1ad86f6a44241832314873e8d54?d=identicon&s=25 Ryan Lunde (Guest)
on 2006-04-16 23:08
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 =
1)  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!
B03b44e1aadb59c9515a6433069b3523?d=identicon&s=25 Andy Shen (Guest)
on 2006-04-17 00:31
(Received via mailing list)
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
3b87f1ad86f6a44241832314873e8d54?d=identicon&s=25 Ryan Lundie (Guest)
on 2006-04-17 01:17
(Received via mailing list)
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!
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 01:36
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.
B03b44e1aadb59c9515a6433069b3523?d=identicon&s=25 Andy Shen (Guest)
on 2006-04-17 01:54
(Received via mailing list)
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
B03b44e1aadb59c9515a6433069b3523?d=identicon&s=25 Andy Shen (Guest)
on 2006-04-17 01:57
(Received via mailing list)
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
This topic is locked and can not be replied to.