Skip to content

Conditions qualifier, when association owner is part of the condition #952

Closed
@AlexVPopov

Description

How do you validate the conditions for a has_one/has_many association, if the association owner is part of the condition? Example:

class Foo
  has_many :bars, -> (foo) { where(baz_id: foo.default_baz_id }
  belongs_to :default_baz, class_name: 'Baz'
end

class Bar
  belongs_to :foo
  belongs_to :baz
end

I tried both conditions(where(baz_id: foo.default_baz_id) and conditions(-> (foo) { where(baz_id: foo.default_baz_id }), but I get:

NoMethodError:
  undefined method `default_baz' for #<Class:0x007fc2d6ca7dd0>

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions