Skip to content

Make it easier to set default associations in a Factory #385

@paulcsmith

Description

@paulcsmith

Depends on #331

class ArticleBox < Box
  def initialize
    post { PostBox.create }
  end

  # This is auto-generated by the box. Putting it here just to show rough implementation
  def post
    before_save do
      if post_id.value.blank?
        record_or_id = yield
        id = if record_or_id.is_a?(Avram::Model)
          record_or_id.id
        else
           record_or_id
        end
        post_id id
      end
    end
  end
end

This will most likely require changes to https://github.com/luckyframework/avram/blob/master/src/avram/save_operation.cr so that we have access to an ASSOCIATIONS constant that has a list of associations and the column to use.

Relevant examples:

We'd want to add a add_association_attributes or something. This will be a bit more complex so I'll try to tackle it, but if someone else wants to take a stab at it, post here and I'll try to help if you run into anything

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions