TwilightCoders / active_record-mti

ActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)
MIT License
98 stars 7 forks source link

Fixture loading issue #6

Open nicolalorusso opened 6 years ago

nicolalorusso commented 6 years ago

Hi guys, I have an issue with test fixture. The fixture related to extended model is not loaded into the test DB.

My environment: OSX, Rails 5.1.4, Ruby 2.4.2, active_record-mti 0.3.0.pre.rc4

My code (extract) Migrations:

create_table :rules do |t|
      t.string :regex,             null: false
     ...
      t.timestamps
    end

create_table :rule_products, inherits: :rules do |t|
      t.xxx xxx ....
end

Models:

class Rule < ApplicationRecord
  validates :regex, presence: true
  ...
end

class RuleProduct < Rule
  self.table_name = 'rule_products'
  ...
end

Fixtures:

--- rules.yml
one:
  regex: rule_one.*

two:
  regex: rule_two.*

--- rule_products.yml
one:
  regex: one.*
  otherfield: value1

two:
  regex: two.*
  otherfield: value2

Now in the test, if I use:

rule = rules(:one)

all works fine, but, if I use:

rule = rule_products(:one)

I got the following error:

Minitest::UnexpectedError: ActiveRecord::RecordNotFound: Couldn't find RuleProduct with 'id'=980190962

and, if I check the test DB, the related table is empty.

I'm doing something wrong? Thanks for the help. Best Regards, Nicola

nicolalorusso commented 6 years ago

Nice, I found the source of the issue. Rails, delete the table each time it loads the fixture. So the issue is related to the order (alphabetical by default) in which the fixtures are loaded. In my case rule_products.yml is loaded before rules.yml and this end up with a complete delete of all "rules" when the second (the base one) is loaded.

So, we have to figure out how to force the fixtures order