0

我开始关注https://github.com/cerebris/peeps为 ember 应用程序编写后端。我的麻烦开始了,我想将其owner_id用作表的外键列,User并且在资源中,我希望将关系称为owner引用users.

# app/models/user.rb
class User < ApplicationRecord
  has_many :rentals, foreign_key: :owner_id
end
#app/models/rental.rb
class Rental < ApplicationRecord
    belongs_to :user, foreign_key: :owner_id
end
# app/resources/api/user_resource.rb
class API::UserResource < JSONAPI::Resource
  attributes :user_name, :first_name, :last_name, :email, :is_staff, :is_active, :is_superuser, :last_login, :date_joined
  has_many :rentals
end
# app/resources/api/rental_resource.rb
class API::RentalResource < JSONAPI::Resource
  attributes :title, :description, :city, :category, :image, :bedrooms

  has_one :owner
  filter :user
end

我想知道我需要添加哪些参数has_one :owner来表示这应该链接到用户资源?

4

1 回答 1

0

我认为class_name应该足够了

class API::RentalResource < JSONAPI::Resource
   has_one :owner, class_name: 'User'
于 2021-05-29T20:14:39.050 回答