hitesh israni
hitesh israni

Reputation: 1752

undefined method `update' for #<ActiveRecord::Associations::HasOneAssociation

models associations are as below:

Model I

class TimeLog < ActiveRecord::Base
  has_one :custom_time_field,  :dependent => :destroy
end

Model II

class CustomTimeField <  ActiveRecord::Base
 belongs_to :time_log
end

Error details:

a = TimeLog.find(1)
a.custom_time_field

#returns => #<CustomTimeField id: 1, time_entry_id: 1, status: 'incomplete', start_time: "2000-01-01 11:24:00", end_time: "2000-01-01 11:24:00">

a.custom_time_field.update(1, :status => '') # returns undefined method `update'

However a.custom_time_field.update_attributes() works

Now i can use update_attributes also i can use save method by creating object

But Why cant i use update method in such case? this is useful when multiple attributes need to updated at a time.

Comments/Pointers?

Upvotes: 1

Views: 1379

Answers (1)

jdoe
jdoe

Reputation: 15771

update is a class method of your model. Call it this way:

CustomTimeField.update(1, :status => '')

Upvotes: 2

Related Questions