2013-07-06 217 views
0

我需要創建一個表單,它將創建另外兩個對象作爲屬性的對象,但這些對象應該可以從包含這些對象模板的下拉列表中獲得。使用嵌套屬性創建對象

class User < ActiveRecord::Base 
    accepts_nested_attributes_for :adresses, :profiles 
end 

class Address < ActiveRecord::Base 
    attr_accessible :city, :country 
    belongs_to :user 
end 

class Profile < ActiveRecord::Base 
    attr_accessible :nickname, :password 
    belongs_to :user 
end 

棘手的部分可能是,該用戶沒有列「ADDRESS_ID」或「profiles_id」,一切都應該去個人資料和地址,這是在同一時刻爲用戶創建(它們具有相同的屬性作爲其模板) 我真的可以使用一些幫助,不要expext全碼解決方案,但一些線索將是很好

回答

2

試試這個設置:

class User < ActiveRecord::Base 
    has_one :address 
    has_one :profile 

    accepts_nested_attributes_for :address, :profile 
    attr_accessible :adress_attributes, :profile_attributes 
end 

class Address < ActiveRecord::Base 
    attr_accessible :city, :country 
    belongs_to :user 
end 

class Profile < ActiveRecord::Base 
    attr_accessible :nickname, :password 
    belongs_to :user 
end 

doc