0

ich einer der Ruby on Rails-Anwendung über GitHub am Beitrag, wo ich das folgende Szenario konfrontiert:Ruby on Rails: Polymorphe Vereinigung Verwirrung

ich folgende Modelle habe, die ich konvertieren wollen polymorphen zu machen:

class Comment < ActiveRecord::Base 
    belongs_to :team 
    belongs_to :user 
    belongs_to :application 
    belongs_to :project 
end 

class Team < ActiveRecord::Base 
    has_many :comments 
end 

class Project < ActiveRecord::Base 
    has_many :comments, -> { order('created_at DESC') }, dependent: :destroy 
end 

class User < ActiveRecord::Base 
end 

class Application < Rails::Application 
end 

ich Änderungen folgende es polymorphe zu machen:

team_id, zu entfernt Perform Datenbankänderung project_id, application_id und user_id und hinzugefügt commentable_id und commentable_type bis comments Tabelle.

Änderungen in Modellen wie beschrieben in Schienen Führungen .:

class Comment < ActiveRecord::Base 
    belongs_to :commentable, polymorphic: true 
end 

class Team < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

class Project < ActiveRecord::Base 
    has_many :comments, as: :commentable, -> { order('created_at DESC') }, dependent: :destroy 
end 

Während ich es mit Standardbereich verwenden, es erlaubt mich nicht, mit Standardbereich zu verwenden und geben Fehler mit folgenden Zeilen:

has_many :comments, as: :commentable, -> { order('created_at DESC') }, dependent: :destroy 

ich bin verwirrt in folgenden Modellen zu ändern:

class User < ActiveRecord::Base 
end 

class Application < ActiveRecord::Base 
end 

Soll ich folg brauchen aufgrund von Änderungen in User und Application Modell?

class User < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

class Application < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

Vielen Dank im Voraus!

+1

Benötigt Ihr Benutzer/Anwendungsobjekt Kommentare. Wenn ja, füge es hinzu – Shishir

+0

Hast du versucht 'has_many: comments,: through =>: commentable'? –

Antwort

0

, wenn Ihre Benutzer/Anwendungsobjekt benötigt Kommentare hinzufügen dann

class User < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

class Application < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

sonst belongs_to/has_many Beziehung nicht polymorph Eg erstellen.

class User < ActiveRecord::Base 
    has_many :comments 
end 

class Application < ActiveRecord::Base 
    has_many :comments 
end