1

I'm new to Rails and I just wondered if there were any protected names you should avoid using in your models? For example, would the following be valid:

class CreateModel < ActiveRecord::Migration
  def change
    create_table :model do |t|
      t.string :hash
      t.integer :count

      t.timestamps
    end
  end
end

I realise has probably isn't a great name for a property, but it's a pure example.

Edit: All the responses were good, but I've chosen my accepted answer because it contains a link to a huge list of protected attributes.

squarefrog
  • 4,750
  • 4
  • 35
  • 64

4 Answers4

3

Avoid class names, if they are defined:

!!defined? Class  # => true
!!defined? Model  # => false

Avoid column names within this list:

  • id - Reserved for primary keys
  • lock_version
  • type - single table inheritance and must contain a class name
  • table_name_count - Reserved for counter cache
  • position - Reserved for acts_as_list
  • parent_id - Reserved for acts_as_tree
  • lft - Reserved for acts_as_nested_set
  • rgt - Reserved for acts_as_nested_set
  • quote - Method in ActiveRecord::Base which is used to quote SQL
  • template
Vitalyp
  • 1,069
  • 1
  • 11
  • 20
1

I can remember only two:

  1. type, because Rails treats this property as a type of parent object in polymorphic objects.
  2. order and any other SQL commands/statements/etc., because Rails generating SQL queries is using them and usually an exception occurs.
Maksim Gladkov
  • 3,051
  • 1
  • 14
  • 16
1

I personally ran into an issue where I named my model record:

Other than that:

Community
  • 1
  • 1
wpp
  • 7,093
  • 4
  • 33
  • 65
1

I had an issues with an external db and a column named "hash". The offending column can be ignored in this manner:

class SomeClass < ActiveRecord::Base
  class << self # Class methods
    alias :all_columns :columns 

    def columns 
      all_columns.reject {|c| c.name == 'hash'} 
    end 
  end 
end