Ruby on rails Rails-同一模型之间具有多对多关联的两个关联
我的问题与非常相似,但有一些问题上的差异 我想在用户和服务之间建立两种关系——所有权的一对多关系和储蓄的多对多关系 我的服务模式:Ruby on rails Rails-同一模型之间具有多对多关联的两个关联,ruby-on-rails,many-to-many,database-migration,Ruby On Rails,Many To Many,Database Migration,我的问题与非常相似,但有一些问题上的差异 我想在用户和服务之间建立两种关系——所有权的一对多关系和储蓄的多对多关系 我的服务模式: 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_nam
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_服务时,我收到以下错误:
PG::UndefinedTable:错误:关系服务\u用户不存在
第1行:从服务内部联接中选择服务。*
您使用的服务
我放弃了数据库并重新构建了它,但这并没有帮助我
不幸的是,它也不起作用。您真的想这样命名此关联:@user.saved_services和@service.saving_users,因为约定是@user.services和@service.users。。我可以根据惯例或您的要求解释您是否创建了HABTM表格服务\用户?您是否创建了UserServiceSave模型?如果是这样,您需要将has_和_-belish_-to_-many关联更改为has_-many:through=>:user_-service_-save关联。嗯,好吧,现在可以了,但我不确定我做错了什么。也许我在git中搞砸了,从另一个版本复制了错误消息。不过,谢谢。