Problem with the catalog integration test

3 views
Skip to first unread message

pmf015

unread,
Oct 3, 2010, 9:43:44 PM10/3/10
to Beginning Ruby on Rails E-Commerce
Hello.

I've encountered strange error while running the integration test on
page 118, and I could really use some help.

ActiveRecord::StatementInvalid (Mysql::Error: Unknown column
'komponentas_proizvodjacs.komponentum_id' in 'on clause': SELECT
count(DISTINCT komponentas.id) AS count_all FROM komponentas LEFT
OUTER JOIN
komponentas_proizvodjacs ON komponentas_proizvodjacs.komponentum_id =
komponentas.id LEFT OUTER JOIN proizvodjacs ON proizvodjacs.id =
komponentas_proizvodjacs.proizvodjac_id LEFT OUTER JOIN kategorijas
ON kategorijas.id = komponentas.kategorija_id ):

1) Failure:
test_browsing_the_site(BrowsingAndSearchingTest)

[c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/
assertions/response_assertions.rb:26:in `assert_response'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/
assertions/response_assertions.rb:18:in `assert_response'
test/integration/browsing_and_searching_test.rb:18:in `browse_index'
test/integration/browsing_and_searching_test.rb:7:in
`test_browsing_the_site'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/
integration.rb:453:in `run']:
Expected response to be a <:success>, but was <500>
1 tests, 1 assertions, 1 failures, 0 errors

Since that unknown "..komponentum_id" is autocreated, I don't know how
to fix it.

This is most of index method for the controller.
(same as in the book, except for the names, komponentas = books,
proizvodjacs = authors, kategorija=publisher)
...
@komponenta_pages, @komponentas = paginate :komponentas,
:per_page => 10,
:include => [:proizvodjacs, :kategorija],
...

If anyone can help with this I would really appreciate it.

Jarkko Laine

unread,
Oct 6, 2010, 5:51:20 AM10/6/10
to railsec...@googlegroups.com
Sorry about the late reply, the message was stuck in Google Groups' spam filter.

On 4.10.2010, at 4.43, pmf015 wrote:

> Hello.
>
> I've encountered strange error while running the integration test on
> page 118, and I could really use some help.
>
> ActiveRecord::StatementInvalid (Mysql::Error: Unknown column
> 'komponentas_proizvodjacs.komponentum_id' in 'on clause': SELECT
> count(DISTINCT komponentas.id) AS count_all FROM komponentas LEFT
> OUTER JOIN
> komponentas_proizvodjacs ON komponentas_proizvodjacs.komponentum_id =
> komponentas.id LEFT OUTER JOIN proizvodjacs ON proizvodjacs.id =
> komponentas_proizvodjacs.proizvodjac_id LEFT OUTER JOIN kategorijas
> ON kategorijas.id = komponentas.kategorija_id ):

This means that there is no field called komponentum_id in table komponentas_proizvodjacs.

> Since that unknown "..komponentum_id" is autocreated, I don't know how
> to fix it.

What do you mean autocreated? Rails migrations only create the id field automatically (and timestamps if you so choose). You have to create foreign keys yourself.

You can check whether the field really exists by writing this in the mysql command line console:

describe komponentas_proizvodjacs;

Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://odesign.fi

Check out my latest book, Unobtrusive Prototype, fresh off the Peepcode oven:
http://peepcode.com/products/unobtrusive-prototype-js

pmf015

unread,
Oct 6, 2010, 12:22:32 PM10/6/10
to Beginning Ruby on Rails E-Commerce
Hi.

Thank You for such promptly reply.

I've only used id's for komponentas and proizvodjacs.

komponentas_proizvodjacs is a join table, and it only has 2 fields:
komponenta_id and proizvodjac_id

So my guess is that that query should be
komponentas_proizvodjacs.komponenta_id not ...komponentum_id

I don't know should I use table and fields names that are a bit
shorter.

Anyway, I've moved over that error by removing :proizvodjacs from that
include.
I know it's not great and that it will increase number of queries, but
the performance is not that important, at least not for the moment.

Still, I would like to know what is wrong with that :include thing and
how to fix it.

Thank You for Your time.

Cheers.

Jarkko Laine

unread,
Oct 7, 2010, 3:59:41 AM10/7/10
to railsec...@googlegroups.com

On 6.10.2010, at 19.22, pmf015 wrote:

> Hi.
>
> Thank You for such promptly reply.
>
> I've only used id's for komponentas and proizvodjacs.
>
> komponentas_proizvodjacs is a join table, and it only has 2 fields:
> komponenta_id and proizvodjac_id
>
> So my guess is that that query should be
> komponentas_proizvodjacs.komponenta_id not ...komponentum_id

Rails (or ActiveRecord, to be precise) uses automatic name inflection
to map between plural words (table names, for example) and singular
words (class name, foreign key name). So unless you state otherwise,
Rails assumes the foreign key name is the name of the corresponding
table in singular + _id. So if your table name is komponentas, Rails
tries to change that to singular. Generally (and obviously) Rails uses
English grammar but since English has many words from Latin, there are
some inflections that assume words are in Latin. It seems like in your
case it assumes that the singular form of komponentas is komponentum.

There are two ways around this. In greenfield projects, I would
generally advise to use English names for everything. It will save you
from many headaches like this and has an added benefit of not
restricting your code to speakers of your native language.

If you can't (or don't want to) change that, you can add your own
inflections to your Rails project: http://apidock.com/rails/ActiveSupport/Inflector/Inflections

Please be aware that the exact syntax may be different depending on
your Rails version.

Just to be clear, this has nothing to do with using :include in
queries. You should hit the same problem whenever you use the
association between komponentas and proizvodjac (such as
@komponenta.proizvodjacs).

Cheers,
//jarkko

>
> I don't know should I use table and fields names that are a bit
> shorter.
>
> Anyway, I've moved over that error by removing :proizvodjacs from that
> include.
> I know it's not great and that it will increase number of queries, but
> the performance is not that important, at least not for the moment.
>
> Still, I would like to know what is wrong with that :include thing and
> how to fix it.
>
> Thank You for Your time.
>
> Cheers.
>

> --
> You received this message because you are subscribed to the Google
> Groups "Beginning Ruby on Rails E-Commerce" group.
> To post to this group, send email to railsec...@googlegroups.com.
> To unsubscribe from this group, send email to railsecommerc...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/railsecommerce?hl=en
> .
>

http://www.railsecommerce.com
http://odesign.fi

pmf015

unread,
Oct 8, 2010, 9:35:47 AM10/8/10
to Beginning Ruby on Rails E-Commerce
Thanks a lot.

I will definitely use only English names in the future.
And as soon I find some spare time I will try to make this code work
with Inflections.

Best regards
Reply all
Reply to author
Forward
0 new messages