我开始关注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
来表示这应该链接到用户资源?