所以,我对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