Skip to content

Commit

Permalink
Merge pull request #46 from zenspongebob/update-readme
Browse files Browse the repository at this point in the history
Add readme entry for many to many associations
  • Loading branch information
bquorning authored Dec 22, 2022
2 parents ff74e56 + 658513a commit 3319b73
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ You need to specicfy `type` option
end
```

### Many to many associations

Please note that this library doesn't work properly with `has_and_belongs_to_many` associations.

Our recommendation is to be explicit and instead use the `has_many, through: join_class` association.
Notice that for it to work properly you must specify the `has_many` to the join class in the main classes of the association,
and your join class must have `belongs_to` main classes defined as shown in the example below:

```ruby
class User < ActiveRecord::Base
include ULID::Rails
ulid :id, auto_generate: true

has_many :user_articles
has_many :articles, through: :user_articles
end

class UserArticle < ActiveRecord::Base
include ULID::Rails
ulid :id, auto_generate: true
ulid :user_id
ulid :article_id

belongs_to :user
belongs_to :article
end

class Article < ActiveRecord::Base
include ULID::Rails
ulid :id, auto_generate: true

has_many :user_articles
end
```

## Development

### Run tests
Expand Down

0 comments on commit 3319b73

Please sign in to comment.