Rails 4 find or create by method doesn't work

后端 未结 3 1044
逝去的感伤
逝去的感伤 2021-01-31 14:03

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 auto

3条回答
  •  情书的邮戳
    2021-01-31 14:44

    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
    

提交回复
热议问题