Ruby on Rails Thursday, October 16, 2014

On Thu, 16 Oct 2014 09:58:16 -0400
Brian Sammon <rubyonrails-list@brisammon.fastmail.fm> wrote:

> > All my tables/models have a "revision" field, and I have a
> > before_update callback to increment the revision field.
> >
> > Among my tables are:
> > events
> > rooms
> > events_rooms
> >
> > In my Event model, I have:
> > has_many :eventRooms
> > has_many(:rooms, :through => :eventRooms)
> >
> > When I create an event, assign a room or two, and then save, the
> > "events_rooms" table entry is initially created with a revision of 1,
> > and then (immediately, automatically, and inexplicably) updated to hav
> > a revision of 2. I do not want every new record to have a revision
> > number of 2. The revision 2 version is identical to revision 1 (which
> > it replaces), aside from the revision number.

My workaround is to do my before_update as follows:
before_update :incr_rev

private
def incr_rev
if changed?
self.revision = self.revision + 1
end
end

With this workaround, it still calls the before_update callback when I
create a new record, but doesn't actually do an update.

I'm baffled that it would be calling the before_update callback if it's
not changed, but it does.

Starting to wonder if I've found a bug.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/20141016134846.9dcdbe4bb5df8eb48b7ae15b%40brisammon.fastmail.fm.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment