2010-12-11 6 views
5

Ich habe diese Klassen:Wie db: ein Modell und all seine verschachtelten Modelle seed?

class User 
    has_one :user_profile 
    accepts_nested_attributes_for :user_profile 
    attr_accessible :email, :password, :password_confirmation, :user_profile_attributes 
end 

class UserProfile 
    has_one :contact, :as => :contactable 
    belongs_to :user 
    accepts_nested_attributes_for :contact 
    attr_accessible :first_name,:last_name, :contact_attributes 
end 

class Contact 
    belongs_to :contactable, :polymorphic => true 
    attr_accessible :street, :city, :province, :postal_code, :country, :phone 
end 

Ich versuche, einen Datensatz in allen drei Tabellen wie folgt einzufügen:

consumer = User.create!(
    [{ 
    :email => '[email protected]', 
    :password => 'aaaaaa', 
    :password_confirmation => 'aaaaaa', 
    :user_profile => { 
     :first_name => 'Gina', 
     :last_name => 'Davis', 
     :contact => { 
     :street => '221 Baker St', 
     :city => 'London', 
     :province => 'HK', 
     :postal_code => '76252', 
     :country => 'UK', 
     :phone => '2346752245' 
    } 
    } 
}]) 

Ein Datensatz in users Tabelle eingefügt wird, aber nicht in die user_profiles oder contacts Tabellen. Kein Fehler tritt auch nicht auf.

Was ist der richtige Weg, um so etwas zu tun?

(dank @Austin L. für den Link) GELÖST

params = { :user => 
    { 
    :email => '[email protected]', 
    :password => 'aaaaaa', 
    :password_confirmation => 'aaaaaa', 
    :user_profile_attributes => { 
     :first_name => 'Gina', 
     :last_name => 'Davis', 
     :contact_attributes => { 
      :street => '221 Baker St', 
      :city => 'London', 
      :province => 'HK', 
      :postal_code => '76252', 
      :country => 'UK', 
      :phone => '2346752245' 
      } 
     } 
    } 
} 
User.create!(params[:user]) 

Antwort

3

Ihr Benutzermodell über accepts_nested_attributes

verschachtelte Attribute akzeptieren Für weitere Informationen siehe die Rails Dokumentation muss eingerichtet werden und Beispiele: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Bearbeiten: Sie könnten auch in Betracht ziehen, has_one :contact, :through => :user_profile zu verwenden, mit dem Sie auf die c zugreifen können ontact wie folgt: @contact = User.first.contact.

Edit 2: Nach dem Spielen um in rails c die beste Lösung, die ich dies finden:

@c = Contact.new(#all of the information) 
@up = UserProfile.new(#all of the information, :contact => @c) 
User.create(#all of the info, :user_profile => @up) 

bearbeiten 3: Sehen Sie sich die Frage nach einer besseren Lösung.

+0

Ja, es ist alles eingerichtet (ich habe bereits die Ansichten und sie funktionieren alle gut). Aber warum scheitert es in db: Seed ist das Problem – Zabba

+0

hast du die 'accepts_nested_attributes' aus deinen Modellen entfernt, wenn du hier posten willst? –

+0

Ja, ich habe nur den Kern der Modelle gepostet. Ich werde alle Details hinzufügen. Entschuldigung :) – Zabba

Verwandte Themen