2015-06-20 41 views
0

林通過軌道4,5型號的has_many混淆,我有3種型號在透視模型中是如何工作的有很多通過關係

簡介:

has_many :profile_services 
has_many :services, through: :profile_services 

服務:

has_many :profile_services 
has_many :profiles, through: :profile_services 

profile_service:

belongs_to :profile 
belongs_to :service 
在我的服務模式

因此有名稱和描述,並在我的profile_service有價格,因爲我需要當用戶創建配置文件自動有3個服務,他正好可以給一個價格爲您服務

而且這個邏輯對我來說很複雜,因爲我認爲我不需要服務控制器,因爲我將使用種子創建此信息,只需要爲profile_service創建一個控制器,因爲用戶爲這些服務添加了價格,但我不知道如何創建配置文件時自動創建這3個服務...

有什麼建議嗎?只是爲了清除我的心靈

感謝您的時間!

回答

0

您可以使用ActiveRecord callbacks在您的模型中執行任何所需的初始化操作。

class Profile < ActiveRecord::Base 
    after_create :setup_services 

    def setup_services 
    # create appropriate services for the profile 
    end 
end 
+1

喔感謝@Dimitris,所以,裏面方法是這樣的: '@profile_services = @ profile.build_services'? 並在服務視圖中工作?類似於: '<%= f.simple_fields_for:profile_services do | builder | %>' –

相關問題