How work with the pivot model in has many through relation

305 views Asked by At

Im confuse with has_many through models in rails 4, I have 3 models

profile:

has_many :profile_services 
has_many :services, through: :profile_services

service:

has_many :profile_services
has_many :profiles, through: :profile_services

profile_service:

belongs_to :profile
belongs_to :service

so in my service model have name and description, and in my profile_service have price because I need when the user create the profile automatically have 3 services and he just can give a price for your services

And this logic is complicated for me because I think I don't need service controller because I will create this info with a seed just need create a controller for profile_service because the user add a price for these services but I don't know how to create these 3 services automatically when create the profile...

any suggestions? just to clear my mind

thanks for your time !

1

There are 1 answers

1
Dimitris Zorbas On

You may use ActiveRecord callbacks to perform any required initialization operations in your models.

class Profile < ActiveRecord::Base
  after_create :setup_services

  def setup_services
    # create appropriate services for the profile
  end
end