Rails - exclude attribute saving

I have a column called updated_at in postgres. I am trying to set db by default. But Rails is still executing an update_at = NULL request. But postgres will only set the default timestamp when update_at is not in the request at all.

How do I Rails exclude a column?

+3
source share
2 answers

You can disable this behavior by setting the variable ActiveRecord :: Base class record_timestamps to false.

In config / environment.rb, Rails :: Initializer.run block:

config.active_record.record_timestamps = false

(if this does not work, try ActiveRecord :: Base.record_timestamps = false at the end of the file instead)

If you want to install only for this model:

class Foo < ActiveRecord::Base
  self.record_timestamps = false
end

Credit Jean-Francois at http://www.ruby-forum.com/topic/72569

0

Rails 2.2.2. , ActiveRecord attr_readonly, create , . , . , .

def create
    if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
      self.id = connection.next_sequence_value(self.class.sequence_name)
    end

    quoted_attributes = attributes_with_quotes(true, false)

    statement = if quoted_attributes.empty?
      connection.empty_insert_statement(self.class.table_name)
    else
      "INSERT INTO #{self.class.quoted_table_name} " +
      "(#{quoted_attributes.keys.join(', ')}) " +
      "VALUES(#{quoted_attributes.values.join(', ')})"
    end

    self.id = connection.insert(statement, "#{self.class.name} Create",
      self.class.primary_key, self.id, self.class.sequence_name)

    @new_record = false
    id
end

, false _with_quotes quoted_attributes.keys SQL. . , , , before_create after_create, , , . - / , .

0

All Articles