0

I have a bookroom model

class BookRoom < ApplicationRecord
  has_many :room_customers
  has_many :rooms, :through => :room_customers
  after_update :add_dynamic_change, :on => :show 

  private 

  def add_dynamic_change
    room = Room.find self.room
    room.update_column :availability, true
  end   
end

 (0.1ms)  begin transaction
  CACHE (0.0ms)  SELECT  "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ?  [["id", 17], ["LIMIT", 1]]
  -> SQL (1.2ms)  UPDATE "rooms" SET "availability" = 't' WHERE "rooms"."id" = ?  [["id", 17]]
  SQL (0.3ms)  INSERT INTO "room_customers" ("created_at", "updated_at", "book_room_id", "room_id") VALUES (?, ?, ?, ?)  [["created_at", 2016-08-19 13:40:11 UTC], ["updated_at", 2016-08-19 13:40:11 UTC], ["book_room_id", 219], ["room_id", 17]]
   (2.0ms)  commit transaction

From: /Users/chineduabalogu/work/hotel-management/app/controllers/book_rooms_controller.rb @ line 20 BookRoomsController#create:

    13: def create 
    14:     @customer = Customer.find(params[:customer_id]) 
    15:     @customer_room = @customer.book_rooms.create(book_rooms_params)
    16:   @room = Room.find @customer_room.room 
    17:   require 'pry'; binding.pry
    18:   @book_rooms = BookRoom.where room_number: @room.room_number
 -> 19:   @room.book_rooms << @customer_room
 => 20:   require 'pry'; binding.pry 
    21:     flash[:notice] = "Customer has been added to room"
    22:     redirect_to customer_path(@customer)
    23: end 

Are there any collection methods that dont update the database? because "<<" this one does an update I dont need. I tried @room.book_rooms.build @customer_room.attributes but I dont know how i can save it

Prince Abalogu
  • 385
  • 2
  • 4
  • 17
  • What exactly are you trying to do here? I've been thinking about this and seems weird. You are trying to update rooms availability status from the BookRoom model when BookRoom is updated only via the show view? – rii Aug 17 '16 at 22:18
  • actually im just trying to change the room.availability to true after I update a BookRoom record...... this is the main thing im trying to achieve.... but because adding to the collection does some update to the BookRoom then my after_update is called there too but i dont want that if you get what i mean? so its more like I dont want it to call the after_update callback when when calling the code on line "->" – Prince Abalogu Aug 18 '16 at 11:27

1 Answers1

0

Well then, from what I can think right now, one potential solution which I am not a huge fan off but could work for you would be to have a attr_accessor :skip_some_callbacks in your BookRoom model similar to the explanation given in this solution: https://stackoverflow.com/a/7386222/664675 . Then in your show action in your controller you can set this 'fake attribute' to true so that your callback is skipped.

Your model would look like this

  class RoomsController < ApplicationController
    attr_accessor :skip_some_callbacks

Your callback method you would then look like this: after_update :add_dynamic_change, on: :show , unless: :skip_some_callbacks

Also here is the best explanation I've ever read on attr_accessor https://stackoverflow.com/a/4371458/664675

Community
  • 1
  • 1
rii
  • 1,578
  • 1
  • 17
  • 22
  • Yeah but my problem is in the "<<" method... for some reason it calls an update... how can I add the :skip_some_callbacks to it.. ive checked all the collection methods. build doesnt call to database but doesnt save either so im quite confused – Prince Abalogu Aug 19 '16 at 13:20
  • I understand, did you try the potential solution out? – rii Aug 19 '16 at 14:23
  • yh I added "class RoomsController < ApplicationController attr_accessor :skip_some_callbacks" and "@room.book_rooms.skip_some_callbacks << @customer_room" its says undefined method skip_some_callbacks for # – Prince Abalogu Aug 19 '16 at 16:10