Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 编辑父列后,是否更新与父列属性的关联?_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 编辑父列后,是否更新与父列属性的关联?

Ruby on rails 编辑父列后,是否更新与父列属性的关联?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我无法使用我的产品更新订阅,也无法将产品的属性复制到与订阅表相同的字段 我的关联是,订阅和产品属于用户,但产品有许多订阅 Subscription.rb class Subscription belongs_to :subscriber, :class_name => "User" belongs_to :subscribable, :polymorphic => true end class Product belongs_to :user has_many :s

我无法使用我的产品更新订阅,也无法将产品的属性复制到与订阅表相同的字段

我的关联是,
订阅
产品
属于
用户
,但
产品
有许多
订阅

Subscription.rb

class Subscription
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true
end
class Product
  belongs_to :user
  has_many :subscriptions, :as => :subscribable, :dependent => :destroy
end
class User
  has_many :products, :dependent => :destroy
  has_many :subscriptions, :foreign_key => :subscriber_id, :dependent => :destroy
end
Product.rb

class Subscription
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true
end
class Product
  belongs_to :user
  has_many :subscriptions, :as => :subscribable, :dependent => :destroy
end
class User
  has_many :products, :dependent => :destroy
  has_many :subscriptions, :foreign_key => :subscriber_id, :dependent => :destroy
end
User.rb

class Subscription
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true
end
class Product
  belongs_to :user
  has_many :subscriptions, :as => :subscribable, :dependent => :destroy
end
class User
  has_many :products, :dependent => :destroy
  has_many :subscriptions, :foreign_key => :subscriber_id, :dependent => :destroy
end
然后是
产品
订阅
表,其中的列与我尝试复制的列相同:

create_table :products do |t|
   t.string  :name
   t.decimal :price
   t.integer :user_id
end

create_table :subscriptions do |t|
   t.string  :name
   t.decimal :price
   t.integer :subscriber_id # same as user_id
   t.integer :subscribable_id
   t.string  :subscribable_type
end
产品控制器

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

def update
   @product = Product.find(params[:id])
   if @product.update_attributes(params[:product])
      redirect_to(@product, :notice => 'Successfully Updated.')   
   else 
      render :back
   end
end
class ProductObserver < ActiveRecord::Observer
  def after_update(product)
    if self.subscriptions.find_by_subscribable_id_and_subscribable_type(subscribable_id, subscribable_type)
        subscription = Subscription.find_by_subscribable_id_and_subscribable_type(subscribable_id, subscribable_type)
        self.subscription.update_attributes(params[:subscription]).select{ |key, _| Subscription.attribute_names.include? key })
    end
  end
end
ProductObserver

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

def update
   @product = Product.find(params[:id])
   if @product.update_attributes(params[:product])
      redirect_to(@product, :notice => 'Successfully Updated.')   
   else 
      render :back
   end
end
class ProductObserver < ActiveRecord::Observer
  def after_update(product)
    if self.subscriptions.find_by_subscribable_id_and_subscribable_type(subscribable_id, subscribable_type)
        subscription = Subscription.find_by_subscribable_id_and_subscribable_type(subscribable_id, subscribable_type)
        self.subscription.update_attributes(params[:subscription]).select{ |key, _| Subscription.attribute_names.include? key })
    end
  end
end
class ProductObserver
更新后的
应该做的是:

  • 检查特定产品的订阅是否存在,以及是否存在
  • 使用新编辑的产品属性更新当前用户订阅

  • 现在,订阅不会在产品更新时更新。我需要修复此代码的哪些内容才能使其执行此操作?将产品字段复制到其订阅时如何?

    尝试将
    :autosave=>true
    传递到您的关联选项中


    你可以阅读更多关于它的信息。

    不确定这是否只是一个打字错误,但你的观察者是错的
    self
    不是您在observer中的产品。相反,您应该使用
    产品
    (给定参数)

    其次,您对订阅的查找似乎也错了。您正在使用未定义的
    subscribable\u id
    subscribable\u type
    ,因此只使用
    nil
    。我认为您希望使用
    product.id
    'product'
    ,但是您可以迭代该产品的所有订阅<代码>产品。订阅
    返回链接到该产品的所有
    订阅

    最后,如果您要使订阅的
    价格
    名称
    始终与链接的产品保持同步,为什么不改为这样做:

     create_table :products do |t|
       t.string  :name
       t.decimal :price
       t.integer :user_id
    end
    
    create_table :subscriptions do |t|
       t.integer :subscriber_id # same as user_id
       t.integer :subscribable_id
       t.string  :subscribable_type
    end
    
    在您的订阅模式中,您可以

    class Subscription
      belongs_to :subscriber, :class_name => "User"
      belongs_to :subscribable, :polymorphic => true
    
      delegate :name, :price, :to => :subscribable, :allow_nil => true
    end
    

    希望这能有所帮助。

    当我把它放在我的
    产品
    模型中时,什么也没发生<代码>有许多订阅:autosave=>true