Search code examples
ruby-on-railsruby-on-rails-5.2

active_record-acts_as gem updating created_at multiple times


I'm creating a Rails 5.2 e-commerce application. I am using the active_record-acts_as gem to simulate multi-table inheritance that has a parent class named Product and multiple subclasses (Book, Clothing, Electronic...etc) that all act as a Product (they inherit all of Product's attributes and methods).

Each Product can only be created in the context of a Category, which corresponds to the names of Product's subclasses. For a visual, here is my current ProductsController...

class ProductsController < ApplicationController
  before_action :set_category, only: [:new, :create]
  before_action :set_product_type, only: [:new, :create]
  before_action :set_product, only: [:show, :edit, :destroy]

  def show
    authorize @product
  end

  def new
    @product = Product.new(actable: @product_type.new)
    authorize @product
  end

  def create
    @product = @product_type.new product_params
    authorize @product

    @product.category = @category
    @product.user = current_user

    if @product.save 
      redirect_to product_path @product.slug
    else
      render :new
    end
  end

  def edit
    @product = Product.friendly.find(params[:id])
    authorize @product

    @category = @product.category
    set_product_type
  end

  def update
    @product = Product.friendly.find(params[:id])
    authorize @product

    @category = @product.category
    set_product_type

    if @product.update product_params
      redirect_to product_path @product.slug
    else
      render :edit
    end
  end

  def destroy
    authorize @product
    @category = @product.category

    if @product.destroy
      redirect_to category_path(@category)
    else
      redirect_to request.referrer
    end
  end

  private

  def set_category
    @category = Category.friendly.find(params[:category_id])
  end

  def set_product
    # specific comes from active_record_acts-as gem
    @product = Product.friendly.find(params[:id]).specific
  end

  def set_product_type
    @product_type = @category.name.singularize.capitalize.constantize
  end

  def product_params
    params.require(:product).permit(:name, :description, :price_cents, :main_image, actable_attributes: (@product_type.permitted_params))
  end
end  

Everything currently works. However, I had to add this to the product model per this SOF question, although, I modified it to use update instead of new, to correct my specific issue.

ACTABLE_TYPES = %w(Book Supply Electronic etc...)

def build_actable(params)
  raise "Unknown actable_type: #{actable_type}" unless ACTABLE_TYPES.include?(actable_type)
  self.actable.update(params)
end

If the build_actable method is not included, the create action works but update action does not. The error, without build_actable when I try to update a Product (subclass of Product) is...

Cannot build association actable. Are you trying to build a polymorphic one-to-one association?

Main Question

Although my current controller and build_actable method "works", every time I update a Product attribute, it updates updated_at three times...here is the log output...

Book Load (0.7ms)  SELECT  "books".* FROM "books" WHERE "books"."id" = $1 LIMIT $2  [["id", 13], ["LIMIT", 1]]
  ↳ app/controllers/products_controller.rb:45
  Product Update (1.0ms)  UPDATE "products" SET "name" = $1, "updated_at" = $2 WHERE "products"."id" = $3  [["name", "Where Angels Fear to Tread something else "], ["updated_at", "2018-07-26 03:28:30.414635"], ["id", 13]]
  ↳ app/models/product.rb:22
  Product Update (0.7ms)  UPDATE "products" SET "updated_at" = $1 WHERE "products"."id" = $2  [["updated_at", "2018-07-26 03:28:30.417622"], ["id", 13]]
  ↳ app/models/product.rb:22
  Product Update (0.5ms)  UPDATE "products" SET "updated_at" = $1 WHERE "products"."id" = $2  [["updated_at", "2018-07-26 03:28:30.420007"], ["id", 13]]
  ↳ app/models/product.rb:22
   (5.0ms)  COMMIT

My controller should certainly be refactored...but why is it updating updated_at 3 times???

Note: I've tried calling build_actable directly instead of update in the controller...but it does the same thing. I have also removed timestamps from Product's subclasses. I feel like I'm missing something obvious....


Solution

  • I was able to correct the issue of updating timestamps twice by adding touch: false to the acts as association in Product's subclasses...

    acts_as :product, touch: false