Ruby on Rails:在创建父项时使用默认值构建子项 [英] Ruby on Rails: Building a child with default values when its parent is created

查看:42
本文介绍了Ruby on Rails:在创建父项时使用默认值构建子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父子模型关系.在 child 的 migration.rb 中,child 模型的列都有默认值(parent_id 列除外).

I have a parent and child model relationship. In the child's migration.rb, the child model's columns each have default values (except the parent_id column).

当我创建一个新的父对象时,如何才能创建一个子对象并将其与默认值中的数据以及 parent_id 一起保存到其表中?

When I make a new parent object, how can I make it so that a child object is created and saved into its table with the data from the default values along with the parent_id?

我认为这与父模型上的 after_create 之类的东西有关,但我不确定如何设置.

I'm thinking that it will have to do with something like an after_create on the parent model, but I'm not sure how to set it up.

推荐答案

已修改:我修改了答案以使用 before_create 和构建而非创建关联模型.一旦父级被保存,ActiveRecord 机制就会负责保存关联的模型.

Revised: I revised the answer to use before_create and building, not creating, the associated models. The ActiveRecord machinery then takes care of saving the associated models once the parent is saved.

我什至测试了这段代码!

I even tested this code!

# in your Room model...
has_many :doors

before_create :build_main_door

private

def build_main_door
  # Build main door instance. Will use default params. One param (:main) is
  # set explicitly. The foreign key to the owning Room model is set
  doors.build(:main => true)
  true # Always return true in callbacks as the normal 'continue' state
end

####### has_one case:

# in your Room model...
has_one :door
before_create :build_main_door
private
def build_main_door
  # Build main door instance. Will use default params. One param (:main) is
  # set explicitly. The foreign key to the owning Room model is set
  build_door(:main => true)
  true # Always return true in callbacks as the normal 'continue' state
end

已添加...

build 方法由拥有模型的机器通过 has_many 语句添加.由于示例使用了 has_many :doors(模型名称 Door),因此构建调用是 door.build

The build method is added by the owning model's machinery by the has_many statement. Since the example uses has_many :doors (model name Door), the build call is doors.build

请参阅有关 has_many 的 文档has_one 查看添加的所有其他方法.

See the docs for has_many and has_one to see all of the additional methods that are added.

# If the owning model has
has_many :user_infos   # note: use plural form

# then use
user_infos.build(...) # note: use plural form

# If the owning model has
has_one :user_info     # note: use singular form

# then use
build_user_info(...) # note: different form of build is added by has_one since
                     # has_one refers to a single object, not to an 
                     # array-like object (eg user_infos) that can be 
                     # augmented with a build method

Rails 2.x 引入了关联的自动保存选项.我认为它不适用于上述情况(我使用的是默认值).自动保存测试结果.

Rails 2.x introduced the autosave option for associations. I don't think it applies to the above (I'm using default). Autosave testing results.

这篇关于Ruby on Rails:在创建父项时使用默认值构建子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆