In our Rails 3.2.6 application, we have a model attribute called "version" that increases at the database level whenever an instance is created or updated (via a trigger). However, after a successful creation or update, the new value specified by the database is not returned to our instance unless we explicitly do the .reload instance.
Does anyone know how an active record automatically reloads an instance after saving to the database?
Background
In our Rails 3.2.6 application, we have 3 tables, each of which has a “version” column. In all three tables, the header “version” should be increased during each insert and update, and “version” should never be repeated.
A simple solution: we created a sequence_ version in postgres that all 3 tables share. We have a small trigger that updates the version column of any record from these tables with the value nextval ('version_sequence'):
-- Resource Version Sequence CREATE SEQUENCE resource_versions_seq; -- Resource Version Trigger CREATE FUNCTION update_resource_version() RETURNS trigger AS $update_resource_version$ BEGIN NEW.version := nextval('resource_versions_seq'); RETURN NEW; END; $update_resource_version$ LANGUAGE plpgsql;
However, when we create, save, or update attributes in active_record, the new version value does not reload after saving the record.
source share