0
我正在構建一個Project應用程序,我需要自動生成1個參與者創建項目記錄。在創建父記錄後自動創建關聯記錄
My model
class Project < ActiveRecord::Base
has_many :participants, dependent: :destroy, inverse_of: :project
after_create :build_a_role
private
def build_a_role
self.participant.create!(user_id: current_user.id, level: 1, participant_cat: @role.id, added_by: current_user.id)
end
end
當我嘗試這個,我得到這個錯誤:
undefined method `participant' for #<Project:0x007fb402707250>
啊,謝謝。我的項目確實將current_user存儲爲user_id ...來訪問它們,我會寫self.participants.create!(user_id::user_id級別:1,participant_cat :: role,added_by :: user_id)? – NothingToSeeHere
'self.participants.create!(user_id:self.user_id,level:1,participant_cat:self.role,added_by:self.user_id)' 將我的回覆標記爲一個,如果它回答您的問題。 – Humza