内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/2394489/passing-extra-data-to-find-or-create
我一直想知道的是,rails的功能是将额外的数据传递给rails中的find_or_create方法.例如,我不能执行以下操作
User.find_or_create_by_name('ceilingfish', :email => 'an_email@a.domain', :legs => true, :face => false)
我可以
u = User.find_or_create_by_name('ceilingfish')
u.update_attributes(:email => 'an_email@a.domain', :legs => true, :face => false)
但是这样比较丑陋,还需要三个查询.我想我可以做
User.find_or_create_by_name_and_email_and_face_and_legs('ceilingfish','an_email@a.domain',true, false)
但是那种意味着我知道电子邮件,腿和脸的价值是什么.有人知道有没有一个非常优雅的方式呢?
尝试这个:
User.find_or_create_by_name(:name=>'ceilingfish',
:email => 'an_email@a.domain', :legs => true, :face => false)
当您有其他参数find_or_create_by_时,必须将所有参数作为散列传递.
轨道4
User.create_with(
email: 'an_email@a.domain',
legs: true, face:false
).find_or_create_by(:name=>'ceilingfish')
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/2394489/passing-extra-data-to-find-or-create
以上所述就是小编给大家介绍的《ruby-on-rails – 将额外的数据传递给find_or_create》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Four
Scott Galloway / Portfolio / 2017-10-3 / USD 28.00
NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!