简体   繁体   中英

Rails - set a db field to null from another object's controller

I am attempting to set a Cabinet's Device's attributes to null in a disconnect method. cabinets_controller calls @cabinet.devices.destroy(@device) and that works ok. Before I do that I want to set @device.row_id = nil and @device.position = nil . They are both Fixnum and attr_accesible in the Device model. They are not being altered in the DB when I call this method. Is there a method to call on @device to make this happen?

Thanks.

I'm guessing that you're trying to break the connection between a @cabinet and a @device without completely deleting the @device. If you're already sure that @device belongs to @cabinet, it might be more straightforward to do something like:

@device.row_id = nil
@device.position = nil
@device.cabinet_id = nil
@device.save!

Basically you'll need to call .save or .save! on @device after setting those other fields to nil; while you're at it you may as well set cabinet_id that way, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM