views:

138

answers:

3

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.

+3  A: 

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.

giraff
Thanks, but this didn't work. Somehow RoR doesn't find the hidden_columns method. I'm gettiing a undefined method.
Hibri
It should work now.
giraff
A: 

Answer is same as above

Method needs to be defined as a class method http://gist.github.com/504745

Hibri
Great that you fixed it yourself. "Above" changes though, so I modified my answer.
giraff
A: 

Another option, would be to make a view that hides the column(s). I've done this when bolting a rails app onto a legacy DB in SQL Server. This way you don't have to mess with / fight rails, and can hide any columns that you want easily...

Doon