cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 533 forks source link

Add option to specify custom method for has_many relationships creation #1386

Open chumakoff opened 2 years ago

chumakoff commented 2 years ago

Currently has_many relations are added to a parent record via ActiveRecord's collection<< method. And there is no way to change that.

So, there should be a way to specify (and implement on API level) a custom method that appends has_many relationships.

Example

# post_resource.rb

has_many :tags, create_method: :add_tag

def add_tag(tag)
  @model.post_tags.create!(tag: tag) if @model.acceptable_tag?(tag)
end