Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3628 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

"quote" cannot be used as a column name

Reported by: berthold Assigned to: David
Priority: high Milestone: 1.2
Component: ActiveRecord Version: 1.1.1
Severity: normal Keywords: quote, activerecord, naming conventions
Cc: dreamer3@gmail.com, dev.rubyonrails.org@tylerrick.com

Description

Not sure if this is really a defect or just something to be aware of, but activerecord's "quote" method apparently excludes the possibility of using "quote" as a column name.

For example, if I have a table named qods, for quote of the day, and I have "quote' as one of my fields, I receive the following error when I call update_attributes:

ArgumentError (wrong number of arguments (2 for 0)):
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1641:in `quote'

Line 1641 is

quoted[name] = quote(value, column) unless !include_primary_key && column.primary

and of course it's calling the method

      def quote(value, column = nil)
        self.class.connection.quote(value, column)
      end

So activerecord gets confused.

Obviously the simple fix, on the developer's part, is to change the column name to something else, but it seems like "quote" would be a fairly frequently used column name. For example, a sales app may use a field called quote to indicate the quoted price told to a client.

Change History

01/27/06 20:25:28 changed by anonymous

You could access it by doing recordquote?, though this is obviously less than ideal.

02/09/06 07:53:52 changed by BobSilva

  • status changed from new to closed.
  • resolution set to duplicate.

This is a known issue and I hope the following patch will be applied.

Ticket #3005

07/21/06 04:07:20 changed by dreamer3@gmail.com

  • cc set to dreamer3@gmail.com.
  • priority changed from normal to high.
  • version changed from 1.0.0 to 1.1.1.
  • milestone set to 1.2.

A much better solution would be to simply rename "quote" to "quote_string" or simliar... or another name less likely to conflict with a top-level modem (that was my issue - why I'm here).

I can make this patch, is someone will apply it... anyone awake?

07/21/06 04:08:14 changed by anonymous

  • status changed from closed to reopened.
  • resolution deleted.

07/21/06 04:09:13 changed by dreamer3@gmail.com

  • status changed from reopened to closed.
  • resolution set to duplicate.

(follow-up: ↓ 7 ) 04/04/07 21:17:28 changed by TylerRick

  • cc changed from dreamer3@gmail.com to dreamer3@gmail.com, dev.rubyonrails.org@tylerrick.com.
  • status changed from closed to reopened.
  • resolution deleted.

Just trying to bring this ticket up-to-date...

It looks like the actual resolution was "Fixed by [5007]", not "duplicate of #3005".

I was looking all around to see if this had actually been fixed and fortunately I stumbled across this in the CHANGELOG...

> svn blame http://dev.rubyonrails.org/svn/rails/trunk/activerecord/CHANGELOG | grep 'Rename AR::Base#quote so people can use that name in their models. #3628 \[Koz\]'
  5007      nzkoz * Rename AR::Base#quote so people can use that name in their models. #3628 [Koz]

> svn log --revision 5007 http://dev.rubyonrails.org/svn/rails/trunk
------------------------------------------------------------------------
r5007 | nzkoz | 2006-09-04 16:41:13 -0700 (Mon, 04 Sep 2006) | 2 lines
Rename quote to quote_value so the name can be used in AR models. #3628 [Koz]

So it looks like koz went with berthold's suggestion after all...

A much better solution would be to simply rename "quote" to "quote_string" or similar... or another name less likely to conflict with a top-level [method].

Just wanted to make sure we had a reference to changeset [5007] in here for future reference.

(I'm guessing Trac would have picked it up automatically if Koz had said "Fixes #3628" instead of just "#3628". (See trac-post-commit-hook))

(in reply to: ↑ 6 ) 04/04/07 22:43:23 changed by TylerRick

Replying to TylerRick:

So it looks like koz went with berthold's suggestion after all...

Correction: dreamer3 's suggestion

04/04/07 22:47:59 changed by TylerRick

So now that this problem has been resolved for "quote", I wonder how many other likely column names are reserved and will cause strange errors like

"ArgumentError (wrong number of arguments (2 for 0))" if you try to use them...?

François Beausoleil listed a couple others that he thinks are still a problem on the Rails list (here), but I haven't confirmed any of them:

./script/../config/../vendor/rails/railties/lib/commands/runner.rb:27:
Columns 'name', 'transaction', 'quote' have already been taken in
models of class Game.  You will have to rename your columns to not
conflict with pre-existing method names.
(ActiveRecord::ColumnNameAlreadyTaken)


(Funny story: Not sure how many others have run into this, but back in the pre-1.2 days, we ended up renaming a column from quote to quote_value to avoid this problem, only to find when we recently upgraded to 1.2 that now quote_value (the "safe" name we renamed to avoid this error) now produces the very error we were trying to avoid! :-) )

I propose that a list of forbidden column names be added to the documentation at

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

or that some more helpful error message be issued if you try to use a reserved column name (see #3005).

06/25/07 19:00:28 changed by matt

  • status changed from reopened to closed.
  • resolution set to fixed.

The original problem has been fixed [5007] so I'll go ahead and close this ticket.