-
Notifications
You must be signed in to change notification settings - Fork 76
Associations versioning
Vladimir Dementyev edited this page Jan 16, 2018
·
2 revisions
Logidze has a feature of associations versioning. The best way to explain it is by the example:
class Post < ActiveRecord::Base
has_many :comments
has_logidze
end
class Comment < ActiveRecord::Base
belongs_to :post
has_logidze
end
# 2017-01-19
post = Post.create!(post_params)
# 2017-01-22
comment = post.comments.create(body: 'My comment')
# 2017-01-24
comment.update!(body: 'New text')
# at this time comment wasn't updated yet...
old_post = post.at('2017-01-23')
# ...so we see its original body, although post itself wasn't changed
old_post.comments.first.body #=> 'My comment'
# at this time post didn't have comments at all
very_old_post = post.at(time: '2017-01-20')
very_old_post.comments.length #=> 0
This feature is considered experimental, so it's disabled by default. You can turn it on by setting Logidze.associations_versioning
to true
. Associations versioning works with belongs_to
and has_many
associations.
- You could notice the usage of the
length
method instead of thesize
in the example above. Because ActiveRecord fires SQL COUNT request in thesize
, if association wasn't loaded in memory, it bypasses logidze, and returns a not versioned result. Thelength
, in turn, always loads association at first, so we can apply versioning here - The
empty?
,any?
,many?
,blank?
methods will always load association in memory. So thecollection_ids
does too - Persisting methods, such as
update_all
,collection_ids=
and etc., are left as they are, so they will change current associations records in the database, no history changing -
at!
method doesn't take into account associations