0

我打算使用Profile這些Sub_profiles之間的多態性關係,允許每個User有自己的基本Profile然後有他們適當Sub_profile

這是我打結的地方。

我有一個具有挑戰性的時間搞清楚的belongs_tohas_one :profileas: :sub_profilepolymorphic: truedependent: :destroy方法都屬於。

在這一點上,理解如何構建這種類型的解決方案的人將能夠寫出關係 - 但我覺得我需要解釋我的(有缺陷的)推理,以便像我這樣做,以便有人能夠幫助我明白我爲什麼做錯了。

我的問題:

同樣重要的是要注意,以防萬一它是不是很明顯,那下面的代碼結構,不會導致根據需要(如上所述),實際工作代碼。我有這樣的錯誤:

> Coach_profile.create 
RuntimeError: Circular dependency detected while autoloading constant Coach_profile 

,並試圖像:

> user.profile.build_coach_profile 

結果undefined method build_coach_profile for profile

背後的原因我的代碼:

我有我的代碼的方式現在建立,我不知道如何build_sub_profilesbuild_coach_profile(作爲一個例子)beca使用我設計的方式,我的關係不允許這樣做。

  • 我希望我的Profilesbelongs_to :sub_profile, polymorphic: true, dependent: :destroy因爲我希望這是我的profilesub_profile_id,所以我可以
    • 參考profile.sub_profile
    • 有不同類型的sub_profiles
    • 銷燬相關sub_profilesprofile被銷燬
  • 我希望我的sub_profilescoachstudent配置文件)has_one :profile, as: :sub_profile
    • sub_profile類型的,這樣兩者可以通過我的profile表的sub_profile_idsub_profile_type領域屬於profile

user.rb

class User < ActiveRecord::Base 

    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_one :profile 

end 

profile.rb

# id    :integer   not null, primary key 
# user_id   :integer 
# first_name  :string(255)  not null 
# middle_name  :string(255) 
# last_name  :string(255)  not null 
# phone_number  :integer 
# birth_date  :date    not null 
# created_at  :datetime 
# updated_at  :datetime 
# sub_profile_id :integer 
# sub_profile_type :string(255) 
# 

class Profile < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :sub_profile, polymorphic: true, dependent: :destroy 

    validates_presence_of :first_name, :last_name, :birth_date 
    validates_length_of :phone_number, {is: 10 || 0} 
end 

coach_profile.rb

# id    :integer   not null, primary key 
# coaching_since :date 
# type_of_coach :string(255) 
# bio   :text 
# created_at  :datetime 
# updated_at  :datetime 
# 

class CoachProfile < ActiveRecord::Base 
    has_one :profile, as: :sub_profile 
end 

student_profile.rb

# id      :integer   not null, primary key 
# playing_since   :date 
# competition_level  :string(255) 
# learn_best_by   :string(255) 
# desirable_coach_traits :text 
# goals     :text 
# bio     :text 
# created_at    :datetime 
# updated_at    :datetime 
# 

class StudentProfile < ActiveRecord::Base 
    has_one :profile, as: :sub_profile 
end 

我在這裏接近錯誤的東西。如何正確設置幾個sub_profiles和父母profile之間的多態關係?

+0

只需在配置文件和子配置文件之間反轉belongs_to has_one,就可以在幾分鐘內找出其他所有內容。 –

+0

我改變了我的子配置文件,以讀取'belongs_to:profile,polymorphic:true',而不是'has_one:profile,as :: sub_profile',並將'as :: sub_profile'部分移到配置文件中的has_one行,因爲它似乎我不能在'belongs_to'調用上使用'as::model'。 然後,我添加了'belongs_to:profile,作爲::sub_profile'到我的sub_profiles的遷移。 嘗試'Coach_profile.create'時,我仍然收到循環依賴錯誤,我想我仍然錯過了一些東西。如果你有任何意見,這將是偉大的,但我會繼續挖掘。 – Ecnalyr

回答

1

你確定這裏的關係是問題嗎?你說你打電話Coach_profile.create,但Rails類的名字應該是CoachProfile沒有下劃線,它看起來像你的。 CoachProfile.create是否給出同樣的錯誤?

您可能只是混淆了使用它們時自動加載源文件的Rails代碼。

而且,它不會工作切換到資料HAS_ONE:sub_profile邁克爾建議,因爲這將無法弄清楚尋找在子型材的表在一個多態關係,類型列與外鍵相同的表上。

+0

CoachProfile.create不會給出相同的錯誤。實際上,它創造了這個對象。現在要弄清楚如何正確調用profile.build_coach_profile的內容。 – Ecnalyr

+0

你可以自己編寫一個build_coach_profile方法;它不會爲您生成(請參閱[自動生成的方法](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html))。你只需要做'profile.sub_profile = CoachProfile.build' – mckeed

+0

你是完全正確的 - 感謝你的自動生成的方法鏈接(一些Rails'魔術',我還沒有想過)。現在,爲了包圍所有這些控制器的喜悅 - 然而,再次,可能比我想象的更容易。 – Ecnalyr

相關問題