paper-trail-gem / paper_trail

Track changes to your rails models
MIT License
6.8k stars 898 forks source link

Association(has_many) with custom version tables #685

Closed kirankumawat closed 8 years ago

kirankumawat commented 8 years ago

I created a table, structure is as given below

class SupplyChainVersion < PaperTrail::Version self.table_name = :supply_chain_versions end Used this table in two models as supply_chain.rb has_paper_trail :class_name => 'SupplyChainVersion', :only => [:compliant_status,:compliant_description]

has_many :supply_chain_requirements // defined relationship

supply_chain_requirement.rb has_paper_trail :class_name => 'SupplyChainVersion'

When I am trying getting child versions with parent using following query ,I am getting wrong records Query is: SupplyChain.find(4).versions.find(6).reify(has_many: true).supply_chain_requirements

with following database queries

SELECT "supply_chains".* FROM "supply_chains" WHERE "supply_chains"."id" = $1 LIMIT 1 [["id", 4]] SupplyChainVersion Load (48.4ms) SELECT "supply_chain_versions".* FROM "supply_chain_versions" WHERE "supply_chain_versions"."item_id" = $1 AND "supply_chain_versions"."item_type" = $2 AND "supply_chain_versions"."id" = $3 ORDER BY "supply_chain_versions"."created_at" ASC, "supply_chain_versions"."id" ASC LIMIT 1 [["item_id", 4], ["item_type", "SupplyChain"], ["id", 4]] SupplyChainVersion Load (1.1ms) SELECT "supply_chain_versions".* FROM "supply_chain_versions" WHERE (id IN (SELECT MIN(version_id) FROM "version_associations" INNER JOIN "versions" ON "versions"."id" = "version_associations"."version_id" WHERE (foreign_key_name = 'supply_chain_id') AND (foreign_key_id = 4) AND (supply_chain_versions.item_type = 'SupplyChainRequirement') AND (created_at >= '2016-01-06 12:34:42.932583' OR transaction_id = 2) GROUP BY item_id)) SupplyChainRequirement Load (1.3ms) SELECT "supply_chain_requirements".* FROM "supply_chain_requirements" WHERE "supply_chain_requirements"."supply_chain_id" = $1 [["supply_chain_id", 4]]

But same scenario is working fine without custom version tables

jaredbeck commented 8 years ago

Please try using our bug report template. You can paste it in here, or use a gist. Thanks!

kirankumawat commented 8 years ago

@jaredbeck should I change the same file or create a new one in the same folder

jaredbeck commented 8 years ago

Please try using our bug report template.

should I change the same file or create a new one in the same folder

Neither. Please paste your code in here, in this issue, or include a link to a gist.

kirankumawat commented 8 years ago

@jaredbeck Gist bug 685 .Please view and tell me if need more info.

jaredbeck commented 8 years ago

Thanks, but when I run your bug report, I get uninitialized constant ActiveRecord (NameError) because you omitted the setup steps from the template.

kirankumawat commented 8 years ago

I updated the template

jaredbeck commented 8 years ago

Hi, sorry for taking a while to get back to you. I tried running your bug report again, but it still has problems. For example, it did not load bundler, and it tried to create json columns in sqlite. Are you actually running it? The purpose of the bug report script is to be run, sorry if that wasn't clear. Anyway, I've fixed a few of the issues for you.

https://gist.github.com/jaredbeck/16d27d1cbdf7f4e2ae9dee7de97975f9

Please run that with ruby pt_issue_685.rb.

jaredbeck commented 8 years ago

Closed due to inactivity. Please let us know when/if you update your bug report script, and we'll be happy to re-open this issue, thanks!