Skip to content
  • eileencodes's avatar
    a332dd93
    Allow Active Record to derive the association query constraints · a332dd93
    eileencodes authored
    Active Record already has the ability to guess the foreign key. With
    query constraints we can use the information from the owner class and
    that foreign key to infer what the query constraints should be for an
    association. This change improves the ergonomics of the query
    constraints feature by making it so that you only have to define the
    query constraints on the top level model and skip defining it on the
    association. There are a few caveats to this as of this PR:
    
    - If the query constraints on the parent are greater than 2, Rails can't
    derive the association constraints
    - If the query constraints on the parent don't include the primary key,
    Rails can't derive the association constraints.
    - If the association has an explicit `foreign_key` or
    `query_constraints` option set, Active Record won't infer the key.
    - I have not implemented support for CPK yet as it's more complex and
    maybe not possible since we don't know which key to split on and use
    for the foreign association.
    
    Prior to this change you would need to do the following to use query
    constraints:
    
    ```ruby
    class Post
      query_constraints :blog_id, :id
    
      has_many :comments, query_constraints: [:blog_id, :blog_post_id]
    end
    
    class Comment
      query_constraints :blog_id, :id
    
      belongs_to :blog, query_constraints: [:blog_id, :blog_post_id]
    end
    ```
    
    After this change, the associations don't require you to set the query
    constraints, Active Record will generate `[:blog_id, :blog_post_id]`
    foreign key itself.
    
    ```ruby
    class Post
      query_constraints :blog_id, :id
    
      has_many :comments
    end
    
    class Comment
      query_constraints :blog_id, :id
    
      belongs_to :blog
    end
    ```
    a332dd93
    Allow Active Record to derive the association query constraints
    eileencodes authored
    Active Record already has the ability to guess the foreign key. With
    query constraints we can use the information from the owner class and
    that foreign key to infer what the query constraints should be for an
    association. This change improves the ergonomics of the query
    constraints feature by making it so that you only have to define the
    query constraints on the top level model and skip defining it on the
    association. There are a few caveats to this as of this PR:
    
    - If the query constraints on the parent are greater than 2, Rails can't
    derive the association constraints
    - If the query constraints on the parent don't include the primary key,
    Rails can't derive the association constraints.
    - If the association has an explicit `foreign_key` or
    `query_constraints` option set, Active Record won't infer the key.
    - I have not implemented support for CPK yet as it's more complex and
    maybe not possible since we don't know which key to split on and use
    for the foreign association.
    
    Prior to this change you would need to do the following to use query
    constraints:
    
    ```ruby
    class Post
      query_constraints :blog_id, :id
    
      has_many :comments, query_constraints: [:blog_id, :blog_post_id]
    end
    
    class Comment
      query_constraints :blog_id, :id
    
      belongs_to :blog, query_constraints: [:blog_id, :blog_post_id]
    end
    ```
    
    After this change, the associations don't require you to set the query
    constraints, Active Record will generate `[:blog_id, :blog_post_id]`
    foreign key itself.
    
    ```ruby
    class Post
      query_constraints :blog_id, :id
    
      has_many :comments
    end
    
    class Comment
      query_constraints :blog_id, :id
    
      belongs_to :blog
    end
    ```
Loading