【问题标题】:How to link form after creating rails join table创建rails连接表后如何链接表单
【发布时间】:2012-07-03 15:08:29
【问题描述】:

您好,我的 Rails 3.1 应用中有一个产品模型,如下所示:

+----------------+---------------+------+-----+---------+----------------+
| Field          | Type          | Null | Key | Default | Extra          |
+----------------+---------------+------+-----+---------+----------------+
| id             | int(11)       | NO   | PRI | NULL    | auto_increment |
| type           | text          | YES  |     | NULL    |                |
| title          | text          | YES  |     | NULL    |                |
| description    | text          | YES  |     | NULL    |                |
| price          | text          | YES  |     | NULL    |                |
| img_src        | text          | YES  |     | NULL    |                |
| source         | text          | YES  |     | NULL    |                |
| sr_id          | text          | YES  |     | NULL    |                |
| categories     | text          | YES  |     | NULL    |                |
+----------------+---------------+------+-----+---------+----------------+

我使用以下迁移创建了 Categories_Products(没有创建模型):

class CreateCategoriesProducts < ActiveRecord::Migration
  def change
    create_table :categories_products, :id => false do |t|
      t.references :product
      t.text :categories
      t.timestamps
    end
  end
end

1) 我如何设置我的产品表单,以便在填写 Categories text_field 时,它会更新我刚刚创建的连接表。我从产品表中删除了类别列。

2) 我这样做的全部原因是因为我最初在一个字段中有多个类别 ID,并且需要将它们分解,以便我可以轻松地执行不同的计数等。用户需要能够为每个产品添加多个类别,我如何告诉 Rails 将添加的每个类别保存到数据库中的新行中?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1


    【解决方案1】:

    一个产品可以有多个分类,一个分类可以引用多个产品,对吧?如果是这样,您想创建第三个关联表,我们称之为product_categories,并使用标准的 Rails 习惯用法来支持它:

    # file: app/models/product.rb
    class Product < ActiveRecord::Base
      has_many :categories, :through => :product_categories
      has_many :product_categories, :dependent => :destroy
    end
    
    # file: app/models/category.rb
    class Category < ActiveRecord::Base
      has_many :products, :through => :product_categories
      has_many :product_categories, :dependent => :destroy
    end
    
    # file: app/models/product_category.rb
    class ProductCategory < ActiveRecord::Base
      belongs_to :product
      belongs_to :category
    end
    

    ...以及您的表/迁移:

    # file: db/migrate/xxx_create_products.rb
    class CreateProducts < ActiveRecord::Migration
      def change
        create_table :products do |t|
          ... 
          t.timestamps
        end
      end
    end
    
    # file: db/migrate/xxx_create_categories.rb
    class CreateCategories < ActiveRecord::Migration
      def change
        create_table :categories do |t|
          t.string :name
          t.timestamps
        end
      end
    end
    
    # file: db/migrate/xxx_create_product_categories.rb
    class CreateProductCategories < ActiveRecord::Migration
      def change
        create_table :product_categories do |t|
          t.references :product
          t.references :category
          t.timestamps
        end
      end
    end
    

    这样,“为每个产品添加多个类别”变得容易:

    my_product.categories.create(:name => "toy")
    

    这将创建一个名为“toy”的类别以及关联 my_product 和该新类别的 ProductCategory。如果您想要完整的描述,this Guide 是一个起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-06
      • 2017-08-19
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 2011-01-23
      • 2013-11-15
      相关资源
      最近更新 更多