本文介绍了ActiveRecord的find_or_build_by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行:

  XXX.find_or_build_by_language_id(属性)
 

我发现

  XXX.find_or_initialize_by_language_id(属性)
 

但只设置language_id并且没有其他属性。即使我手动设置的属性,记录当我执行XXX.save不会被保存。

我刚刚看了导轨 - 寻找或创造 - 是有发现或建立,这似乎与我的问题,但是呢?不适合我的需要。

修改

让我们用这个场景

 #DB /迁移/ create_models.rb
类CreateModels< ActiveRecord的::迁移
  高清self.up
    CREATE_TABLE:公司做| T |
      t.string:名称
    结束

    CREATE_TABLE:员工办| T |
      t.string:名称
      t.string:城市
      t.references:公司
    结束
  结束
结束
 

-

 #应用程序/模型/ employee.rb
一流的员工<的ActiveRecord :: Base的
  belongs_to的:公司
结束
 

-

 #应用程序/模型/ company.rb
一流的公司和LT;的ActiveRecord :: Base的
  的has_many:雇员
结束
 

-

 #导轨控制台
:001〕 C = Company.new
 => #<公司ID:无,名:无>
:002> c.employees
 => []
:003> E = c.employees.find_or_initialize_by_name(:名称=>'富',:城市=>巴)
 => #<员工ID:无,名富,市酒吧,的​​company_id:无>
:004> c.employees
 => []
:005> c.save
 =>真正
:006> c.employees
 => []
:007> e.save
 =>真正
:008> C = Company.first
 => #<公司ID:1,名称:无>
:009> c.employees
 => [#<员工ID:1,名称:富,市酒吧,的​​company_id:1>]
:010> E = c.employees.find_or_initialize_by_name(:名称=>'富',:城市=>巴兹)
 => #<员工ID:1,名称:富,市酒吧,的​​company_id:1>
:011> e.city ='巴兹'
 => 巴兹
:012> c.employees
 => [#<员工ID:1,名称:富,市酒吧,的​​company_id:1>]
:013> c.save
 =>真正
:014> c.employees
 => [#<员工ID:1,名称:富,市酒吧,的​​company_id:1>]
 

问题

  1. :004 =>从该员工:003不会被添加到c.employees
  2. :006 =>从该员工:003保存用C
  3. :010 =>员工的城市属性未设置​​
  4. :014 =>员工的城市属性,不更新挽救公司时,
解决方案

这个怎么样?

  employee_attrs = {:名称=> '富',:城市=> '酒吧'}
E = c.employees.where(employee_attrs)。首先|| c.employees.build(employee_attrs)
 

I would like to perform:

    XXX.find_or_build_by_language_id(attributes)

I found

    XXX.find_or_initialize_by_language_id(attributes)

but that only set language_id and no other attributes. Even if I manually sets the attributes, the record is not saved when I perform XXX.save.

I just read Rails - find or create - is there a find or build?, which seems related to my problem but does not fit my needs.

Edit

Let's use this scenario

# db/migrations/create_models.rb
class CreateModels < ActiveRecord::Migration
  def self.up
    create_table :companies do |t|
      t.string :name
    end

    create_table :employees do |t|
      t.string :name
      t.string :city
      t.references :company
    end
  end
end

-

# app/models/employee.rb
class Employee < ActiveRecord::Base
  belongs_to :company
end

-

# app/models/company.rb
class Company < ActiveRecord::Base
  has_many :employees
end

-

# rails console
:001> c = Company.new
 => #<Company id: nil, name: nil>
:002> c.employees
 => []
:003> e = c.employees.find_or_initialize_by_name(:name => 'foo', :city => 'bar')
 => #<Employee id: nil, name: "foo", city: "bar", company_id: nil>
:004> c.employees
 => []
:005> c.save
 => true
:006> c.employees
 => []
:007> e.save
 => true
:008> c = Company.first
 => #<Company id: 1, name: nil>
:009> c.employees
 => [#<Employee id: 1, name: "foo", city: "bar", company_id: 1>]
:010> e = c.employees.find_or_initialize_by_name(:name => 'foo', :city => 'baz')
 => #<Employee id: 1, name: "foo", city: "bar", company_id: 1>
:011> e.city = 'baz'
 => "baz"
:012> c.employees
 => [#<Employee id: 1, name: "foo", city: "bar", company_id: 1>]
:013 > c.save
 => true
:014> c.employees
 => [#<Employee id: 1, name: "foo", city: "bar", company_id: 1>]

Problems

  1. :004 => The Employee from :003 is not added to c.employees
  2. :006 => The Employee from :003 is saved with c
  3. :010 => The city attribute of employee is not set
  4. :014 => THe city attribute of employee is not updated when saving company
解决方案

How about this?

employee_attrs = {:name => 'foo', :city => 'bar'}
e = c.employees.where(employee_attrs).first || c.employees.build(employee_attrs)

这篇关于ActiveRecord的find_or_build_by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:26