簡體   English   中英

如果沒有模型文件,該表如何更新?

[英]how is this table updated if there is no model file?

我正在嘗試在Spree中手動更新數據庫,但是它沒有模型文件。 我想知道如何添加到option_values_variants表中。

該代碼使用James Golick的資源控制器,但我想不用它。

型號/option_value.rb

class OptionValue < ActiveRecord::Base
  belongs_to :option_type
  acts_as_list :scope => :option_type
  has_and_belongs_to_many :variants
end

controllers / admin / variants_controller.rb

class Admin::VariantsController < Admin::BaseController
  resource_controller
  belongs_to :product

  new_action.response do |wants|
    wants.html {render :action => :new, :layout => false}
  end

  create.before :create_before

  # redirect to index (instead of r_c default of show view)
  create.response do |wants| 
    wants.html {redirect_to collection_url}
  end

  # redirect to index (instead of r_c default of show view)
  update.response do |wants| 
    wants.html {redirect_to collection_url}
  end

  # override the destory method to set deleted_at value 
  # instead of actually deleting the product.
  def destroy
    @variant = Variant.find(params[:id])

    @variant.deleted_at = Time.now()
    if @variant.save
      self.notice = I18n.t("notice_messages.variant_deleted")
    else
      self.notice = I18n.t("notice_messages.variant_not_deleted")
    end

    respond_to do |format|
      format.html { redirect_to admin_product_variants_url(params[:product_id]) }
      format.js  { render_js_for_destroy }
    end
  end

  private 
  def create_before 
    option_values = params[:new_variant]
    option_values.each_value {|id| @object.option_values << OptionValue.find(id)}
    @object.save
  end

  def collection
    @deleted =  (params.key?(:deleted)  && params[:deleted] == "on") ? "checked" : ""

    if @deleted.blank?
      @collection ||= end_of_association_chain.active.find(:all)
    else
      @collection ||= end_of_association_chain.deleted.find(:all)
    end
  end
end

我相信這與models / option_value.rb:has_and_belongs_to_many:variants行有關。 但是如何關聯兩個項目?

創建“隱藏”表以跟蹤表之間的關聯(關系)。 (請參閱導軌指南 )。

例如用於創建: @order = @customer.orders.create(:order_date => Time.now)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM