Forum: Ruby on Rails Strange problems with belongs_to()

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.
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-03-25 14:57
(Received via mailing list)
Hey.

I am a bit confused, seems to be simple thing, but anyhow it does not
work for me:

class Foo
  has_many(:bars)
end

class Bar
  belongs_to(:myfoo, :class_name => 'Foo')
end

b = Bar.new().myfoo = Foo.find_by_name('COOKIE_MONSTER')
b.save()

The bar table got a FK field for Foo: "foo_id". As long as I do not
choose an extra relation name, this works. But in this case, when I
name the relation "myfoo" and give informationen about the class name,
ActiveRecord fails an says <Column 'foo' does not exist.> Why is there
no "_id" appended? I also tried to deliver the :foreign_key parameter,
but I did not succeed.

As I said: If I name the relation "foo", all the things work.

Do you have an idea?

Thanks for helping! Cheers, ms
This topic is locked and can not be replied to.