在 Rails 中创建 2 个具有 has_one 关联的对象 [英] Creating 2 objects with has_one association in Rails

查看:38
本文介绍了在 Rails 中创建 2 个具有 has_one 关联的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取一些关于在 Rails 中通过验证创建新对象的信息.例如有如下代码:

I need to get some info about creating new objects in Rails with validation. For example, there is the following code:

def create
  @user = User.new(params[:user])
  if @user.save
    # some actions: redirect, render, etc
  else
    render 'new'
  end
end

但是如果有 2 个具有 has_one 关联的模型,例如 Club 和 Place.我需要在同一个创建"操作中从 params 创建这两个对象,因为我有相同的形式为它输入数据(params[:club]params[:俱乐部][:place]).我不知道我应该如何保存这些对象,因为为了建立一个地方 (@club.build_place(params[:club][:place])) 我应该将 @club 保存在数据库中.请给我示例代码以解决我的问题.提前致谢.

But if there is 2 models with has_one association, for example Club and Place. I need to create both this objects from params in the same 'create' action, because I've got the same form for inputing data for it(params[:club] and params[:club][:place]). I don't know how I should save this objects, because for building a place (@club.build_place(params[:club][:place])) I should save @club in database. Please, give me example of the code for my problem. Thanks in advance.

推荐答案

如果您从单个表单创建多个对象,您可能最好将此逻辑放入表单对象"中...请参阅文章来自 CodeClimate 博客的重构 Fat ActiveRecord 模型的 7 种模式"(查找关于提取表单对象的第 3 节):http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-模型.

If you're creating multiple objects from a single form you'd probably be best off putting this logic into a "Form Object"... See the article "7 Patterns to Refactor Fat ActiveRecord Models" from the CodeClimate blog found here (look for Section #3 on extracting Form Objects): http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models.

Railscasts 在表单对象上也有很好的插曲,尽管它是专业插曲"(即需要订阅).http://railscasts.com/episodes/416-form-objects

Railscasts also has a good episode on form objects, though it is a "Pro Episode" (i.e. requires subscription). http://railscasts.com/episodes/416-form-objects

简而言之,您创建一个自定义模型,包括一些必要的 ActiveModel 模块,然后创建一个自定义保存方法,例如(这是直接来自文章,其中有很多很好的建议).

In short, you create a custom model including some of the necessary ActiveModel modules then create a custom save method, e.g. (this is directly from the article which has a lot of great advice).

class Signup
  include Virtus

  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations

  attr_reader :user
  attr_reader :company

  attribute :name, String
  attribute :company_name, String
  attribute :email, String

  validates :email, presence: true
  # … more validations …

  # Forms are never themselves persisted
  def persisted?
    false
  end

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

private

  def persist!
    @company = Company.create!(name: company_name)
    @user = @company.users.create!(name: name, email: email)
  end
end

这为您提供了更多的控制权和更简洁的界面.

This gives you much more control and a much cleaner interface.

这篇关于在 Rails 中创建 2 个具有 has_one 关联的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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