1

I have following model -

class Project < ActiveRecord::Base
    enum commitment: {
           more_than_30hrs_week: 'More than 30hrs/week',
           less_than_30hrs_week: 'Less than 30hrs/week',
           dont_know: 'Dont know'
       }
    enum status: [:open, :closed, :archived]

    validates :commitment, :status :presence => true
end

When I try to update project object like the following -

@project.update!(project_params)

where -

 @project = #<Project:0x007fde227059d8
               id: 5,
               commitment: "less than 30hrs/week",
               status: 2 >
 project_params =  {"status"=>"archived"}

It throws me following error -

ActiveRecord::RecordInvalid: Validation failed: Commitment can't be blank

I fail to understand why is it throwing this error as commitment value already exists and I am not passing in the attributes to update.

Doing the following works but I do not want it this way as I want to update multiple attributes -

 @project.update_attribute(:status, 'archived')

Can someone please explain, how can I achieve this?

Disha
  • 776
  • 9
  • 19
  • How about `update_attributes` with the hash as parameter? – Eyeslandic Feb 01 '17 at 10:35
  • @Iceman `update_attributes` too gives Validation failed error. – Disha Feb 01 '17 at 10:40
  • Do you have any callbacks in your model that may remove `commitment` when `status` is changed ? – Slava.K Feb 01 '17 at 11:19
  • @Slava.K No! I don't have any callbacks. – Disha Feb 01 '17 at 12:39
  • The real value for key `less_than_30hrs_week` is "Less than 30hrs/week". Why your `#` is showing the value with lowercase 'L'. `'less than 30hrs/week'` is not the value, real value is with capital 'L' `'Less than 30hrs/week'` – Azmat Rana Feb 01 '17 at 13:36
  • Try removing the `enum commitment` block as a whole, the error must be related to that. – Eyeslandic Feb 01 '17 at 16:21
  • @Iceman I didn't change any code. It stopped throwing me that error today. I am puzzled :S – Disha Feb 02 '17 at 11:02
  • Well, something must have changed, either in your code or code you depend on like a gem or something. – Eyeslandic Feb 02 '17 at 11:03
  • Thats exactly what I am trying to figure out. As there can't be any magic here. I reverted to yest's commit and checked. Still worked. Will let you know once I figure out the reason – Disha Feb 02 '17 at 11:06

0 Answers0