-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix retrieving :has_one association #348
Conversation
Instead using `lookback` just get correct version from DB
This looks good to me at a glance. I like some of the cleanup you've done and your logic makes sense as far as I can tell. I haven't done a ton of work on the associations portion of the gem, much of that was in place before I started working with it and hasn't been touched much for a while now, so it's probably in need of some updating. I'm not sure that this will impact this directly, but just so you know, I'm working on a feature that is going to alter the way the scope methods ( |
Using As for now, are there any contraindications/objections not to merge fix from this PR? |
/ping @batter |
@kiela - I released I agree that it makes sense to drop the The aforementioned invalid behavior has been documented in #131 and #354. So I guess that is the next priority for me to work on. Until I can come up with a solution for that, I don't think we can drop the |
dd4280e
to
a683be8
Compare
@kiela - After reading your description more closely just now and the proposed solution you have submitted, I want to pose to you the question as to whether you have tested your proposed solution to ensure it works as expected? As in, have you taken your branch here, employed it in a scenario like the one you described, and checked to see if it works? I still think we have an issue here:
def reify_has_ones(model)
model.class.reflect_on_all_associations(:has_one).each do |assoc|
child = model.send assoc.name
if child.respond_to? :version_at
# we want to grab the associated child as it was immediately PRIOR
# to the timestamp field being passed in here
if child_as_it_was = child.version_at(send(PaperTrail.timestamp_field)).previous_version
child_as_it_was.attributes.each do |k,v|
model.send(assoc.name).send :write_attribute, k.to_sym, v rescue nil
end
else
model.send "#{assoc.name}=", nil
end
end
end
end Or we could even clean this up (and optimize it) by adding some sort of a method to the # works like `version_at` method but grabs previous version (if it exists)
def version_prior_to(timestamp, reify_options={})
v = send(self.class.versions_association_name).preceding(timestamp, true).first
return v.reify(reify_options) if v
end Then we can change the setter line for if child_as_it_was = child.version_prior_to(send(PaperTrail.timestamp_field)) Am I totally offbase here? Can we add some better tests surrounding this usecase? |
@batter I'm a little busy lately so please give me some time and I will get back to this issue as soon as I will be able to. |
@kiela - I just released |
8357c21
to
1ad7838
Compare
Some weird situation came up when I was working with Paper Trail. I have:
Category
andPost
modelsPost
, associated it with oneCategory
and save to the DBIn my controller I have tried to retrieve proper version of the post by executing
Version.find(params[:id]).reify(has_one: true)
. Although this command found proper version ofPost
it wasn't be able to find the proper version ofCategory
. The problem is here: https://github.com/airblade/paper_trail/blob/ffd7bc233a18f1910588bd0c51a572a235da3c98/lib/paper_trail/version_concern.rb#L201 PaperTrail tried to fetch version ofCategory
from max 3 seconds (by default) before chosen version ofPost
was created.I think the better idea is to drop
lookback
option and fetch proper version of:has_one
association by using.version_at
.I know that I need to update
README.md
and comments in the code as well, but firstly I want to get some feedback from the maintainers.