Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell ruby on rails, not to insert to a column?

I have a table, with merge replication on it (SQL Server 2005). There is a rowguid column. I want RoR to ignore this column, and not insert into this, and not include this when generating the INSERT statement.

like image 208
Hibri Avatar asked Oct 15 '22 02:10

Hibri


1 Answers

See this ticket which proposes a patch to rails. You can add the following code to a new file /config/initializers/hidden_columns.rb:

require "activerecord"

class << ActiveRecord::Base 
  def hidden_columns(*hidden) 
    write_inheritable_array("hidden_column", hidden.collect(&:to_s)) 
  end

  def columns_hidden
    read_inheritable_attribute("hidden_column") || [] 
  end 

  def columns 
    unless defined?(@columns) && @columns 
      @columns = connection.columns(table_name, "#{name} Columns").delete_if {|c| columns_hidden.member?(c.name) } 
      @columns.each {|column| column.primary = column.name == primary_key} 
    end 
    @columns 
  end
end

Then you can write:

hidden_columns :rowguid

in the concerned models.

like image 129
giraff Avatar answered Oct 28 '22 17:10

giraff