加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – acceptance_nested_attributes_for链接到现有

发布时间:2020-12-16 20:06:18 所属栏目:百科 来源:网络整理
导读:我有以下型号 class Order AR::Base has_many :products accepts_nested_attributes_for :productsendclass Product AR::Base belongs_to :order has_and_belongs_to_many :stores accepts_nested_attributes_for :storesendclass Store AR::Base has_and_be
我有以下型号
class Order < AR::Base
  has_many :products

  accepts_nested_attributes_for :products
end

class Product < AR::Base
  belongs_to :order
  has_and_belongs_to_many :stores

  accepts_nested_attributes_for :stores
end

class Store < AR::Base
  has_and_belongs_to_many :products
end

现在我有一个订单视图,我想更新产品的商店.
事实是,我只想将产品连接到我的数据库中的现有商店,而不是创建新的商店.

订单视图中的表单看起来像这样(使用Formtastic):

= semantic_form_for @order do |f|
  = f.inputs :for => :live_products do |live_products_form|
    = live_products_form.inputs :for => :stores do |stores_form|
      = stores_form.input :name,:as => :select,:collection => Store.all.map(&:name)

虽然它嵌套它工作正常.
问题是,当我选择商店并尝试更新订单(以及产品和商店)时,Rails尝试使用该名称创建一个新的商店.我想要它只是使用现有的商店和产品连接.

任何帮助谢谢!

编辑1:

最后我以一个非常粗暴的方式解决了这个问题:

# ProductsController

def update
  [...]

  # Filter out stores
  stores_attributes = params[:product].delete(:stores_attributes)

  @product.attributes = params[:product]

  if stores_attributes.present?
    # Set stores
    @product.stores = stores_attributes.map do |store_attributes|
      # This will raise RecordNotFound exception if a store with that name doesn't exist
      Store.find_by_name!(store_attributes[:name])
    end
  end

  @order.save

  [...]
end

编辑2:

巴勃罗的解决方案更加优雅,应该比我的首选.

解决方法

尝试实现:reject_if,检查商店是否已经存在,然后使用它:
class Product < AR::Base
  belongs_to :order
  has_and_belongs_to_many :stores

  accepts_nested_attributes_for :stores,:reject_if => :check_store

  protected

    def check_store(store_attr)
      if _store = Store.find(store_attr['id'])
        self.store = _store
        return true
      end
      return false
    end
end

我有一个这个代码在当前项目中正常工作.

请让我知道,如果你找到一个更好的解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读