Marcus
Marcus

Reputation: 9472

Rails save model Attribute from another Controller

I would like to change a value for a 'device' model from my User controller. Currently I get the device then set the value, but it isn't saving the value to the device. How do I get it to save? thanks

User controller.rb

      @device = @user.device
      @device.lastUpdated = Time(now)

Upvotes: 0

Views: 1370

Answers (1)

hlh
hlh

Reputation: 2072

To save the changes to your database, you can use the save method. Try using @device.save after you update the attribute.

  @device = @user.device
  @device.lastUpdated = Time(now)
  if @device.save
    # do something when save is successful
  else
    # handle the case when it doesn't save properly
  end

Edit: Take a look at the list of persistence methods in the Rails documentation (i.e. these are methods that will write information to the database).

Upvotes: 1

Related Questions