Skip to content
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

Restoring has_many associacions #652

Closed
tm-digitalpatrioten opened this issue Nov 24, 2015 · 4 comments
Closed

Restoring has_many associacions #652

tm-digitalpatrioten opened this issue Nov 24, 2015 · 4 comments

Comments

@tm-digitalpatrioten
Copy link

Hi,
I'm not sure if this is a bug, or if I'm simply doing something wrong. I have two very simple models:

class Template < ActiveRecord::Base
  has_many :assets, dependent: :destroy
  has_paper_trail
  #also has a title
end

class Asset < ActiveRecord::Base
  belongs_to :template
  has_paper_trail
  has_attached_file :file, :path => ':class/:id/:filename.:extension'
  do_not_validate_attachment_file_type :file
end

This is the test I use at the moment:

    template = Template.create

    template.title = 'initial'
    template.assets.create()
    template.save

    title1 = template.title  #initial
    count1 = template.assets.count #1

    template.title = 'v2'
    template.assets.create()
    template.save

    title2 = template.title #v2
    count2 = template.assets.count #2

    template_old = template.versions.last.reify(:has_many => true)

    title3 = template_old.title #initial
    count3 = template_old.assets.count #2, should be 1

    template.title = 'v3'
    template.assets.last.destroy()
    template.save

    title4 = template.title #v3
    count4 = template.assets.count #1

    template_old = template.versions.last.reify(:has_many => true)

    title5 = template_old.title #v2
    count5 = template_old.assets.count #1, should be 2

Thanks in advance!

@jaredbeck
Copy link
Member

Instead of using count, try length.

template_old.assets.length
=> 1
template_old.assets.count
   (2.4ms)  SELECT COUNT(*) FROM "assets" WHERE "assets"."template_id" = ?  [["template_id", 1]]
=> 2

Note how count ignores the reified data and queries the database.

This is surprising behavior, so I'm inclined to call it a bug, but I haven't the slightest idea how to fix it 😢

Associations, and especially the reification of associations, is still an experimental feature in PaperTrail, and I don't recommend using it in production yet.

@tm-digitalpatrioten
Copy link
Author

Hi @jaredbeck,

thanks for answering. I just went through some more testing. After the reify, length and count behave in the same way (right after template_old is written). I also tried eaching over the assets and it was identical with count/length.

I also had a look at the reify_has_many_directly function in version_concern.rb. In line 384 where the collection is put into the model, the collection also reflects the "live" state of the relation rather than the state of the relation at the time of the version.
In 367 the live state of the association is loaded. The only point where elements (that have not existed yet) could be removed from the collection is 374. The strange thing here is that the versions hash in 362 was empty in all my tests. Therefore when mapping over the collection always the first case is selected in 372. I think there is probably something wrong here.

Hope this helped, let me know if i can help with some further testing or debugging.

@jaredbeck
Copy link
Member

Can you please confirm this is still an issue in the latest PT (currently 5.1.1)? Thanks.

@jaredbeck
Copy link
Member

Closing due to lack of activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants