jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
319 stars 63 forks source link

jsonapi:serializable declares `has_one` instead of `belongs_to` #82

Closed mrjonesbot closed 6 years ago

mrjonesbot commented 6 years ago

When defining a serializable resource with the provided generator, jsonapi-rails declares has_one relationships in the serializer despite the resource specifying a belongs_to association.

ruby -v 2.5.0 rails -v 5.1.4

model:

class Attempt < ApplicationRecord
  belongs_to :user, optional: true
  belongs_to :consultant, class_name: 'User', optional: true
  belongs_to :version

  validates :price, presence: true

  monetize :price_cents
end

generator command:

cpp-api resource-crud % rails g jsonapi:serializable Attempt
      create  app/serializable/serializable_attempt.rb
cpp-api resource-crud %

output:

class SerializableAttempt < JSONAPI::Serializable::Resource
  type 'attempts'
  attribute :price_cents
  attribute :price_currency
  attribute :state
  attribute :results
  attribute :created_at
  attribute :updated_at
  has_one :user
  has_one :consultant
  has_one :version
end
mrjonesbot commented 6 years ago

Came across this in the docs:

Relationships

Relationships are declared via the has_many, has_one and belongs_to DSL methods. 
belongs_to is actually an alias for has_one, and you can use both interchangeably.

The generator is working as intended, thank you!