ruby-on-rails - 如何在 JSONAPI::Resource 中执行 `has_one: model, through: join_model`

标签 ruby-on-rails jsonapi-resources

在模型资源中执行 has_one :model, through: join_model 的首选方法是什么?通常 JSONAPI::Resource 期望表/模型上的 model_id 列拥有关联。如果使用连接表/模型,则不存在。

最佳答案

实际上你可以只提到has_many关系,而不需要提到through关联。

所以如果你有这个模型结构:

class Teacher < ActiveRecord::Base
  has_many :classrooms
  has_many :students, through: :classrooms
end

class Student < ActiveRecord::Base
  has_many :classrooms
  has_many :teachers, through: :classrooms
end

class Classroom < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :student
end

在您的Teacher 资源中,您只需要has_many :students

同样,在您的 Student 资源中,您需要 has_many :teachers

关于ruby-on-rails - 如何在 JSONAPI::Resource 中执行 `has_one: model, through: join_model`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002953/

相关文章:

ruby-on-rails - 如何在 Rails 中使用 jsonapi-resorces gem 实现嵌套资源?

php - Laravel API 资源 : How to return infinite nested array of categories with their children?

php - Laravel API 资源隐藏 key

ruby-on-rails - Rspec通过范围验证唯一性

mysql - Ruby on Rails 的 implode 和 explode 函数来改变日期格式

ruby-on-rails - Rspec 中类的未定义方法

ruby-on-rails - JSONAPI 响应非资源数据的最佳方式,如使用 jsonapi-resources 的访问 token

ruby-on-rails - 将 devise_token_auth 与 jsonapi-resources 结合使用

ruby-on-rails - 简单表格 : Is it possible to remove labels and replace them with placeholders using the labels i18n?

ruby-on-rails - Ruby on Rails 按日期时间范围查询(过去 24、48 等...小时)