Reputation: 31
I'm using activeadmin in my RoR webapp. when I create a new user/record, I want to set/update an attribute depend on it's id/pk. e.g his id is 1234, column X should have the value 1234-XXX.
Since there is no activerecord callback for that(i cant set the value with callbacks, because there isnt one, after the entry is created/stored in the db) you to ask, how I could solve this?
Thanks in advance
Upvotes: 2
Views: 1717
Reputation: 151
I think you can use after_commit
callback
after_commit :do_something, on: :create
def do_something
update_column(:column_x, "#{id}-XXX")
end
Upvotes: 2
Reputation: 5609
You can use the after_action
controller callback :
class RecordsController < ApplicationController
after_action :set_columnx, only: [:create, :update]
private
def set_columnx
@record = Record.find(params[:id])
@record.columnx = "#{@record.id}-ABCD"
@record.save!
end
end
Upvotes: 1