How can I update another model with a callback?

I am working with a model called Recover. Before creating the model, I would like to save the boolean attribute Combo.occupied = true using the Recover.combo_id attribute as a reference.

It seems that my SQL is executing the query correctly, but does not save this attribute. How can I keep Combo.occupied = true?

recover.rb:

before_create: checkin

reserved

def checkin x = Combo.find_by_id (combo_id) .occupied = true
end

Rails Console:

The POST "/ is restored" will start for 127.0.0.1 on 2011-01-06 17:07:24 -0800
Processing RecoversController # create as HTML
Parameters: {"utf8" => "✓", "Authenticity_token" => "o1Iu3Y9 / rVBOZPoDUgVP / tRfQ8GxbdWC40DbPq9YxUE = "," Recovery "=> {" combo_id "=>" 4 "," Email "=>" jz@marin.edu "}," commit "=>" Create Recover "} Restore Load (0.2ms ) SELECT "restores". "Id" FROM "is restored" WHERE (is "restored." Email "= ' justin.zollars@marin.edu ') LIMIT 1
Restore load (0.1 ms) SELECT is" restored ". "id "FROM" restored "WHERE (" restored "." combo_id "= 4) LIMIT 1
Combined loading (0.5 ms) SELECT "combo". * FROM "combo" WHERE ("combo". "Id" = 4) LIMIT 1 AREL (0.5 ms) INSERT INTO "is restored" ("locker_number", "email", "requests", "created_at", "updated_at "," combo_id ") VALUES (NULL, ' justin.zollars@marin.edu ', NULL, '2011-01-07 01: 07: 24.287072', '2011-01-07 01: 07: 24.287072', 4) Redirected http: // localhost: 3000 / recovers / 14 Completed 302 found in 119 ms

RecoversController # create

def create @recover = Recover.new (params [: recover])

respond_to do |format|
  if @recover.save
    format.html { redirect_to(@recover, :notice =>

'Recovery was successfully created.')} Format.xml {render: xml => @recover ,: status =>: created,

: location => @recover}

  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @recover.errors, :status =>

: unprocessable_entity}       

end  

+3
1

save , :

def checkin
  combo = Combo.find_by_id(combo_id)
  combo.occupied = true
  combo.save!
end

, update_attribute. , belongs_to, find:

belongs_to :combo

def checkin
  if combo # true unless combo_id is nil
    combo.update_attribute(:occupied,true)
  end
end

, update_attribute . , update_attributes(:occupied=>true).

+5

Source: https://habr.com/ru/post/1783983/


All Articles