jackc / surus

PostgreSQL extensions for ActiveRecord
MIT License
395 stars 35 forks source link

Support json has_many though relationships #35

Closed schpet closed 6 years ago

schpet commented 6 years ago

Supports two types of has_many through:

1) C has a b_id, B has a a_id

    class A < AR::Base
      has_many :bs
      has_many :cs, through: bs
    end

    class B < AR::Base
      belongs_to :a
      has_many :cs
    end

    class C < AR::Base
      belongs_to :b
    end

2) B acts as a join table, similar to HABTM

    class A < AR::Base
      has_many :bs
      has_many :cs, through: bs
    end

    class B < AR::Base
      belongs_to :a
      belongs_to :c
    end

    class C < AR::Base
    end

Fixes #15

schpet commented 6 years ago

Really appreciate this library! Was having trouble figuring out how to directly serve json from postgres and this really helped me out. Please let me know if there's anything I can add or change with this, it's my first time dipping into AR stuff 😨.

A big thanks, and happy October 🎃

jackc commented 6 years ago

Thanks! Looks good. I merged and released a new gem.