0

My problem is quite similar to this, but with some problematic differences.

I'd like to make two relationships between User and Service - one-to-many relationship of ownership and many-to-many relationship of saving.

My Service model:

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

My User model:

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

Trying to get @user.saved_services I receive this error:

PG::UndefinedTable: ERROR: relation "services_users" does not exist LINE 1: SELECT "services".* FROM "services" INNER JOIN "services_use...

I dropped database and built it again, but this didn't help me.

Unfortunately this solution also doesn't work.

Karol Selak
  • 4,248
  • 6
  • 35
  • 65
  • do you really want to name this association like this: `@user.saved_services` and `@service.saving_users` because the convention would be `@user.services` and `@service.users`.. I can explain you based on the convention or based on your requirements – Fabrizio Bertoglio Jun 28 '17 at 16:24
  • 1
    did you create the HABTM table `services_users`? – Michael Gorman Jun 28 '17 at 16:38
  • did you create a UserServiceSave model? if so, you will need to change the `has_and_belongs_to_many` associations to `has_many :through => :user_service_save` associations. – Michael Gorman Jun 29 '17 at 14:19
  • Hm, okay, it works now, but I'm not sure what I was doing wrong. Maybe I did some mess in git and copied error message from another version. However, thanks. – Karol Selak Jun 29 '17 at 15:40

0 Answers0