find_or_initialize_by doesn't work with 2 columns

后端 未结 3 1743
旧时难觅i
旧时难觅i 2021-02-19 22:35

I am trying to insert data in my database using ActiveRecord.

When I use pdays = MyModel.new for initialization rather then the below find_or_initiali

相关标签:
3条回答
  • 2021-02-19 23:09

    You could try:

    MyModel.find_or_initialize_by_local_dt_and_comunity_id([:local_dt], vdays[:community_id])
    

    appending the columns names by with an _and_

    0 讨论(0)
  • 2021-02-19 23:26

    I read here that in Rails 4, this is the correct syntax:

    Progress.find_or_initialize_by(chore_id: chore.id, period: period[chore.frequency], account_id: chore.account_id)
    
    0 讨论(0)
  • 2021-02-19 23:28

    If you are using rails 3.2.1 and above, the new name of the method is first_or_initialize. It is used with conjunction with where. In your case, since you are calling save, you should use first_or_create but if you want to manually call save, just replace the method and call save after

    MyModel.where(local_date: vdays_traffic[:local_date], community_id: vdays_traffic[:community_id]).first_or_create do |record|
      record.uniquesters = vdays_traffic[:uniques]
    end
    
    0 讨论(0)
提交回复
热议问题