70's futuristic technology

Programming focused drivel

Saturday, September 23, 2006

Rails gotcha...
For my database architecture class, I have been implementing a Ruby on Rails site, to try out different ideas for the class project's database design. I ran into an obscure problem, so here is the solution in case someone else hits this same wall.

Associations between objects cannot have name classes with internal Rails method names such as "quote"


originally I had the models Page, Quote, and Comment. Comment had a belongs_to :quote association. Upon saving a comment I got this weird error that it couldn't be saved to the database and all the fields were of type Quote. I suspected that there was a namespace clash, so I renamed Quote to Bquote and belongs_to :quote over to belongs_to :bquote. Then my tests passed. Ouch. Where is the list of reserved keywords which can't be used in a belongs_to?

class Comment < ActiveRecord::Base
belongs_to :quote,
:class_name => "Bquote"

belongs_to :user
acts_as_tree

validates_presence_of :title
validates_presence_of :body
end

Here is a console to confirm my suspicion, where I change bquote back

Sasquatch:~/M/class-project/bookbuddy/trunk ozten$ ./script/console
Loading development environment.
>> c = Comment.new
=> #nil, "title"=>nil, "bquote_id"=>nil, "user_id"=>nil, "parent_id"=>nil}, @new_record=true>
>> c.body = "foo"
=> "foo"
>> c.title = "foo"
=> "foo"
>> c.bquote_id = 1
=> 1
>> c.user_id = 1
=> 1
>> c.parent_id = 0
=> 0
>> c.save
ActiveRecord::StatementInvalid: Mysql::Error: #42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1: INSERT INTO comments (`body`, `title`, `bquote_id`, `user_id`, `parent_id`) VALUES(#, #, #, #, #)
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in `log'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:184:in `execute'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:194:in `insert'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1739:in `create_without_callbacks'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:265:in `create_without_timestamps'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/timestamp.rb:30:in `create'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1718:in `create_or_update_without_callbacks'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:253:in `create_or_update'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1392:in `save_without_validation'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:736:in `save_without_transactions'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in `save'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:91:in `transaction'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:118:in `transaction'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in `save'
from (irb):8

So funny. Oh man. I don't see any naming conflict in the docs, nor on my system, but I can just revert my code back and use belongs_to options to work around this conflict.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home