基本Rails问题 - 如何在数据库中创建新条目?

时间:2010-12-11 05:05:32

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

我有以下型号:

Product: name, shop_id (foreign key), brand_id (foreign key), price
Shop:    name
Brand:   name

协会是:

Product: belongs_to :shop
         belongs_to :brand
Shop:    has_many   :products
         has_many   :brands,   :through => :products
Brand:   has_many   :products
         has_many   :shops,    :through => :products

问题1

这些关联是否有意义?你会添加其他协会吗?

问题2

我想在db/seeds.db预先填充数据库。

要添加ShopBrand我这样做:

Shop.create(:name => shop_name)
Brand.create(:name => brand_name)

添加Product最合适的方法是什么?我是否真的需要手动插入shop_idbrand_id值?如果商店和新创建的产品的品牌尚不存在,它们会自动添加到数据库吗?

2 个答案:

答案 0 :(得分:5)

您所做的关联的一般想法是:

shop = Shop.create(:name => shop_name)
shop.brands << Brand.create(:name => brand_name)

或者相反。如果您不想,则不必手动创建连接模型。

编辑:以下是关于您的评论的演示。

设置迁移。

$ ./script/rails g model Shop name:string
$ ./script/rails g model Brand name:string
$ ./script/rails g model Product brand_id:integer shop_id:integer
$ rm test/fixtures/*

$ rake db:migrate; rake db:test:prepare

模特。

class Brand < ActiveRecord::Base
  has_many :products
  has_many :shops, :through => :products
end

class Shop < ActiveRecord::Base
  has_many :products
  has_many :brands, :through => :products
end

class Product < ActiveRecord::Base
  belongs_to :brand
  belongs_to :shop
end

测试。请注意,没有任何代码行明确创建产品。

require 'test_helper'

class ShopTest < ActiveSupport::TestCase

  def test_brand_assignment_to_shop
    assert_equal 0, Product.count

    shop = Shop.create(:name => "Foo Shop")
    brand = Brand.create(:name => "Foo Brand")
    shop.brands << brand

    assert_equal 1, Product.count
    assert_equal shop.id, Product.first.shop_id
    assert_equal brand.id, Product.first.brand_id
  end
end



$ ruby -I./test test/unit/shop_test.rb 
Loaded suite test/unit/shop_test
Started
.
Finished in 0.029778 seconds.

1 tests, 4 assertions, 0 failures, 0 errors

答案 1 :(得分:1)

在创建产品模型时,根据协会是正确的。

您还可以使用accepts_nested_attributes_for,它允许您通过父级保存相关记录的属性。

对于种子,我想你是手动插入shop_id和brand_id。这些可以按如下方式完成:

@shop = Shop.create(:name => shop_name)
@brand = Brand.create(:name => brand_name)

Product.create(:shop_id => @shop.id , :brand_id => @brand.id)

请记住,首先应该创建父项,然后是子项,所以在插入数据时首先创建商店和品牌,然后创建产品

希望,这可能会解决您的问题