2013-04-08 10 views
6

Ich habe folgendes Datenmodell in meinem Rails 2.3 AnwendungSchienen has_one: through. Gebäude zugehöriges Objekt

class PortraitSubject 
    has_many :portraits 
    has_one  :primary_portrait, :through => :portraits, :source => :asset, :conditions => ['portraits.primary = ?', true] 
    has_many :supplementary_portraits, :through => :portraits, :source => :asset, :conditions => ['portraits.primary = ?', false] 

    ... 
end 

class Portrait 
    belongs_to :portrait_subject 
    belongs_to :asset 

    ... 
end 

ich die dazugehörigen Proxy-Modelle mit Rails bauen will, aber versuchen primary_portrait schlägt mit einer Ausnahme zu bauen. I.e.

# This works 
subject = PortraitSubject.new 
subject.supplementary_portraits.build 
subject.save 

# This doesn't 
subject = PortraitSubject.new 
subject.build_primary_portrait 
# => NoMethodError: undefined method `build_primary_portrait' for #<PortraitSubject:0x007ff16fe38948> 

Ich bin mir nicht sicher, was ich falsch mache. Beim Durchsehen der Rails-Guides sieht es so aus, als ob dies mit einer has_one Beziehung möglich wäre. Jede Hilfe würde sehr geschätzt werden.

+1

sind Sie sicher bauen möchten, können Sie bauen ': through' Verbände? Wenn Sie es sind, geben Sie bitte einen Link an – Zippie

Antwort

0

Warum nicht das Folgende tun.

class Portrait 
    belongs_to :portrait_subject 
    belongs_to :asset 

    ... 
end 

-

class PrimaryPortrait < Portrait 
    ... 
end 

-

class SupplementaryPortraits < Portrait 
    ... 
end 

-

class PortraitSubject 
    has_one  :primary_portrait 
    has_many :supplementary_portraits 
    ... 
end 

Dies folgt Schienen Entwurfsmuster enger. Sie müssen jedoch eine Typspalte hinzufügen.

0

würde ich Splitting dies in zwei Verbände schlagen vor:

class PortraitSubject 
    has_many :portraits 
    has_one  :primary_portrait, :class_name => "Portrait", :conditions => ['portraits.primary = ?', true] 
    has_one  :primary_portrait_asset, :through => :primary_portrait, :source => :asset 

    has_many :supplementary_portraits, :class_name => "Portrait", :conditions => ['portraits.primary = ?', false] 
    has_many  :supplementary_portrait_assets, :through => :supplementary_portraits, :source => :asset 

    ... 
end 

Dann sind Sie subject.build_primary_portrait können das Porträt Modell erstellen und Zugriff auf ihre Asset durch subject.primary_portrait_asset.

1

Sie werden mit diesen Namenskonventionen verrückt werden. A PrimaryPortrait und SecondaryPortrait sollten Sonderfälle von Portrait nicht die Vermögenswerte, die zu einem Portrait gehören. Es bricht schon dein Design, dass du keins bauen kannst.

Try this:

class PortraitSubject 
    has_many :portraits 
    has_one  :primary_portrait, :conditions => {:primary => true} 
    has_many :supplementary_portraits, :conditions => {:primary => false} 

    has_many :portrait_assests, :through => :portraits 
    has_one  :primary_portrait_asset, :through => :primary_portrait 
    has_many :supplementary_portrait_assets, :through => :supplementary_portraits 

end 

dann, wenn Sie eine primary_portait_asset schreiben Sie eine Instanz-Methode

def build_primary_portrait_asset 
    primary_portrait || build_primary_portrait 
    primary_portrait.asset || primary_portrait.build_asset 
end 
Verwandte Themen