Rails 4 find or create by method doesn't work

后端 未结 3 888
逝去的感伤
逝去的感伤 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:55

    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.

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

提交回复
热议问题