轨道4查找或者方法来创建不起作用 [英] Rails 4 find or create by method doesn't work
本文介绍了轨道4查找或者方法来创建不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个就业和企业之间有很多关联,它工作正常。在作业表单视图我已经text_field为公司名称与一个自动完成功能。自动完成工作正常,但如果我把没有在自动完成列表中存在的公司名称find_or_create_by不创建一个新的公司。
高清COMPANY_NAME
company.try(:名称)
结束 高清COMPANY_NAME =(名称)
@company = Company.find_or_create_by(名称:名)
结束
解决方案
请看看这个答案。
曾经被认为是
@company = Company.find_or_create_by_name(名)
在轨道4现在
@company = Company.find_or_create_by(名称:名)
另一种方式在Rails中4要做到这一点是:
@company = Company.where(名称:名).first_or_create
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.
def company_name
company.try(:name)
end
def company_name=(name)
@company = Company.find_or_create_by(name: name)
end
解决方案
Please take a look at this answer.
What used to be
@company = Company.find_or_create_by_name(name)
in Rails 4 is now
@company = Company.find_or_create_by(name: name)
Another way to do this in Rails 4 would be:
@company = Company.where(name: name).first_or_create
这篇关于轨道4查找或者方法来创建不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文