Link to fixtures of the same type in Rails

In associations between different models, you can avoid setting foreign key identifiers directly using device names, as in this answer . What about self-regulation associations, for example when using acts_as_tree ? Attempt:

# categories.yml
forsale:
  name: For Sale
  parent_id: nil

books:
  name: Books
  parent: forsale

I get this error:

SQLite3::SQLException: table categories has no column named parent: INSERT INTO "categories" ("name", "parent") VALUES ('Books', 'forsale')

Is there a way to make one refence fixture different in the same class without using explicit identifiers?

Update:

Adding a class name between parentheses, for example, for polymorphic belongs to_to , also does not work. Performing this action:

books:
  name: Books
  parent: forsale (Category)

Generates random parent_idfor booksinstead of forsaleid.

+3
1

, . , ( (Category)). ? Rails ? Category - :

belongs_to :parent, :class_name => "Category"

, parent_id :

books:
  name: Books
  parent_id: <%= Fixtures.identify :forsale %>

, , ...

+2

Source: https://habr.com/ru/post/1732931/


All Articles