Rails - 具有多对多关联的相同模型之间的两个关联

时间:2017-06-28 16:15:18

标签: ruby-on-rails many-to-many database-migration

我的问题与this非常相似,但存在一些问题。

我想在UserService之间建立两种关系 - 一对多的所有权关系和多对多的保存关系。

我的服务模式:

class Service < ApplicationRecord
  belongs_to :user #ownership
  belongs_to :category
  has_many :time_slots
  has_many :pictures
  has_and_belongs_to_many :saving_users, class_name: 'User' #saving
end

我的用户模型:

class User < ApplicationRecord
  has_many :services #ownership
  has_many :reservations
  has_many :time_slots, through: :reservations
  has_and_belongs_to_many :saved_services, class_name: 'Service' #saving
end

尝试获取@user.saved_services我收到此错误:

  

PG :: UndefinedTable:错误:关系“services_users”不存在   第1行:选择“服务”。* FROM“服务”INNER JOIN   “services_use ...

我删除了数据库并再次构建它,但这对我没有帮助。

不幸的是this solution也不起作用。

0 个答案:

没有答案