通过方法查找或创建Rails 4不起作用

时间:2021-11-23 12:44:57

I have a one to many association between jobs and companies and it works fine. In the job form view I have text_field for the company name with an autocomplete feature. The autocomplete works fine but the find_or_create_by don't create a new company if I put a company name that doesn't exist in the autocomplete list.

我在工作和公司之间有很多联系,而且它很有效。在job表单视图中,我有一个带有自动完成功能的公司名称的text_field。自动补全功能正常,但是如果我在自动补全列表中添加了一个不存在的公司名称,那么find_or_create_by不会创建一个新公司。

  def company_name
    company.try(:name)
  end

  def company_name=(name)
    @company = Company.find_or_create_by(name: name)
  end

3 个解决方案

#1


109  

Please take a look at this answer.

请看这个答案。

What used to be

曾经是什么

@company = Company.find_or_create_by_name(name)

in Rails 4 is now

在Rails 4中是现在

@company = Company.find_or_create_by(name: name)

Another way to do this in Rails 4 would be:

在Rails 4中实现这一点的另一种方法是:

@company = Company.where(name: name).first_or_create

#2


23  

Company.find_or_create_by(name: name)

It should work out of the box. Only thing that can prevent it from creating record is validation errors.

它应该是开箱即用的。唯一可以阻止它创建记录的是验证错误。

Try this in rails console to check if its working or not. And check the validation errors as well.

请在rails控制台尝试查看它是否正常工作。并检查验证错误。

name = "YOUR TEXT FOR NAME ATTRIBUTE"
c = Company.find_or_create_by(name: name)
puts c.errors.full_messages

#3


0  

This method is deprecated in rails 4.

在rails 4中不赞成使用这个方法。

Rails4 release message:

Rails4发布信息:

Deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do.

废弃了旧式的基于散列的查找程序API。这意味着先前接受“查找器选项”的方法不再这样做。

It is deprecated method, but it should work. Maybe, Rails is not supporting it now.

它是不赞成的方法,但是应该可以工作。也许,Rails现在不支持它了。

You can get more info click here and check 11.2 Deprecations.

您可以在这里获得更多的信息,并检查11.2的弃用。

#1


109  

Please take a look at this answer.

请看这个答案。

What used to be

曾经是什么

@company = Company.find_or_create_by_name(name)

in Rails 4 is now

在Rails 4中是现在

@company = Company.find_or_create_by(name: name)

Another way to do this in Rails 4 would be:

在Rails 4中实现这一点的另一种方法是:

@company = Company.where(name: name).first_or_create

#2


23  

Company.find_or_create_by(name: name)

It should work out of the box. Only thing that can prevent it from creating record is validation errors.

它应该是开箱即用的。唯一可以阻止它创建记录的是验证错误。

Try this in rails console to check if its working or not. And check the validation errors as well.

请在rails控制台尝试查看它是否正常工作。并检查验证错误。

name = "YOUR TEXT FOR NAME ATTRIBUTE"
c = Company.find_or_create_by(name: name)
puts c.errors.full_messages

#3


0  

This method is deprecated in rails 4.

在rails 4中不赞成使用这个方法。

Rails4 release message:

Rails4发布信息:

Deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do.

废弃了旧式的基于散列的查找程序API。这意味着先前接受“查找器选项”的方法不再这样做。

It is deprecated method, but it should work. Maybe, Rails is not supporting it now.

它是不赞成的方法,但是应该可以工作。也许,Rails现在不支持它了。

You can get more info click here and check 11.2 Deprecations.

您可以在这里获得更多的信息,并检查11.2的弃用。