0

I created an instance variable (@edit_id) in the user_controller the update action.

def update
  ......
  @edit_id = 1
  ......
end

and I have an Active Record Callbacks in the user_model

has_many :details
after_update :create_user_details
  ......
  private
  def create_user_details
     detail = user.details.build
     detail.edit_id = @edit_id
     #(this @edit_id is the user_controller update action 
     #created instance variable, but **@edit_id is nil**)
     detail.save
  end

I how use the instance variable in the callback action

1 Answer 1

2

You cannot use instance variables from your controller in your model.
They both are different instances of different objects and therefore don't share instance variables.

You need to provide your model with this value by setting it in it's instance like this :

model Foo
  attr_accessor :edit_id

  private
  def create_user_details
   detail = user.details.build
   detail.edit_id = edit_id
   detail.save
  end
end

Then, in your controller, you specify the value to the model and save it.

def update
  @foo = Foo.new
  @foo.edit_id = 1
  # Whatever other business logic you want to do
end
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.