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
2010-07-30 18:01:48
Thanks, but this didn't work. Somehow RoR doesn't find the hidden_columns method. I'm gettiing a undefined method.
Hibri
2010-08-02 12:40:07
It should work now.
giraff
2010-08-05 19:54:54
A:
Answer is same as above
Method needs to be defined as a class method http://gist.github.com/504745
Hibri
2010-08-02 14:48:58
Great that you fixed it yourself. "Above" changes though, so I modified my answer.
giraff
2010-08-03 15:56:14
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
2010-08-02 15:15:34