所以,我對Rails相當陌生,並且由於模型的複雜性而陷入困境。Rails 5使用父對象通過使用嵌套屬性創建多個子對象
我有一個Developer
模型,Township
模型和Project
模式,其內容如下: -
Developer.rb
Class Developer < ApplicationRecord
has_many :townships,
has_many :projects, through: :townships
accepts_nested_attributes_for :township
end
Township.rb
Class Township < ApplicationRecord
belongs_to :developer
has_many :projects
accepts_nested_attributes_for :project
end
Project.rb
Class Project < ApplicationRecord
belongs_to :township
end
我想有這樣如下創建項目: -
project = Developer.create(
{
name: 'Lodha',
township_attributes: [
{
name: 'Palava',
project_attributes: [
{
name: 'Central Park'
},
{
name: 'Golden Tomorrow'
}
]}
]})
任何想法,我怎麼能做到這一點?我還需要了解DeveloperController
中所需的強參數白名單。
是否正確定義了您的關聯? – rogelio
從我的理解,他們是。 –
沒問題,但關係如下:1)'Developer'是多對多的'Project'或者2''Developer''具有很多'Township'''''''很多'項目' – rogelio