如何为has_and_belongs_to_many关系设置我的灯具?

时间:2022-10-04 15:56:35

I have the following models:

我有以下型号:

class Company < ActiveRecord::Base
  has_and_belongs_to_many :regions

class Region < ActiveRecord::Base
  has_many :requests
  has_and_belongs_to_many :companies

class RequestForProposals < ActiveRecord::Base
  belongs_to :region

Whenever I get a new request, I want to send a notification to the companies active in the same region.

每当我收到新请求时,我都会向同一地区的公司发送通知。

How do I set this up in my fixtures so that I can unit test the logic of finding the right companies?

如何在我的灯具中进行设置,以便我可以单独测试找到合适公司的逻辑?

I've tried

我试过了

region_ids: 1, 2
regions: one, two

in companies.yml, but neither works in assigning regions to the companies.

在companies.yml中,但都不适用于为公司分配区域。

Here is a gist of the SQL generated: https://gist.github.com/2713518

以下是SQL生成的要点:https://gist.github.com/2713518

4 个解决方案

#1


30  

For

对于

regions: one, two

in companies.yml to work you need to let rails auto assign the ids for the regions. This is because (in order to avoid having to have read regions.yml before companies.yml) rails calculates the ids it sticks in the join table from the names of the companies fixtures. if you've assigned the ids yourself, they won't match up with the calculated ones.

在companies.yml工作,您需要让rails自动为区域分配ID。这是因为(为了避免在companies.yml之前必须读取regions.yml),rails会根据公司灯具的名称计算它在连接表中的ID。如果您自己分配了ID,则它们将与计算出的ID不匹配。

From the sql you've provided it looks like you're setting the ids on the regions to 1 and 2, i.e. your regions.yml has

从您提供的sql看起来,您将区域上的ID设置为1和2,即您的regions.yml具有

one:
  id: 1
  name: MyString 

Remove the id:1 and you should be ok. You'll also need to update any other files (e.g. request_for_proposals.yml) that refer to regions, replacing

删除id:1,你应该没问题。您还需要更新引用区域的任何其他文件(例如request_for_proposals.yml)

region_id: 1

with

region: one

Rails will know that means to set region_id to the id for the region with label one in your fixtures.

Rails会知道这意味着将region_id设置为你的灯具中标签为1的区域的id。

#2


0  

Is your regions.yml in test/fixtures directory? How about companies.yml? Your code "regions: one, two", for a given company, should work.
Rails automatically loads the fixtures in this directory when you run the test. If the locations are correct, please post the test output - sql generated - when you run the tests.

你的regions.yml是test / fixtures目录吗? companies.yml怎么样?对于给定的公司,您的代码“区域:一,二”应该有效。运行测试时,Rails会自动加载此目录中的灯具。如果位置正确,请在运行测试时发布测试输出 - 生成的sql。

#3


0  

I'm unsure how does one use YAML fixtures these days. Have you tried FactoryGirl for creating db object instances while testing? It does pretty much the same as fixtures, in a much sophisticated way though.

我不确定这些天如何使用YAML灯具。您是否尝试过FactoryGirl在测试时创建数据库对象实例?它与固定装置几乎完全相同,但却非常复杂。

The following passages assumes you are using rspec as a testing framework.

以下段落假设您使用rspec作为测试框架。

After including factory_girl-rails in your Gemfile and updating spec/spec_helper.rb accordingly to the factory_girls README, create the following files:

在Gemfile中包含factory_girl-rails并相应地更新spec / spec_helper.rb到factory_girls自述文件后,创建以下文件:

# spec/factories/company_factories.rb
FactoryGirl.define do
  factory :company do
    title { |n| "Test Company #{n}" }

    # whatever else fields

    after_create { |company| company.regions << FactoryGirl.create(:region) }
  end
end

# spec/factories/region_factories.rb
FactoryGirl.define do
  factory :region do
    # whatever fields here
  end
end

# spec/factories/request_factories.rb
FactoryGirl.define do
  factory :request do        
    # whatever fields here
  end
end

Now the real question is - what's the actual test code you're doing?

现在真正的问题是 - 你正在做的实际测试代码是什么?

#4


0  

In addition to accepted answer if you have a need to calculate such id by yourself, e.g. you have some loose references or another datasource just add:

如果你需要自己计算这样的id,除了接受的答案,例如你有一些松散的引用或另一个数据源只是添加:

def fixh(key)
  ActiveRecord::FixtureSet.identify key
end

to your test_helper.rb and then use this way in fixtures:

到你的test_helper.rb,然后在灯具中使用这种方式:

security_context1:
  ext_id: <%= fixh :user1 %>

#1


30  

For

对于

regions: one, two

in companies.yml to work you need to let rails auto assign the ids for the regions. This is because (in order to avoid having to have read regions.yml before companies.yml) rails calculates the ids it sticks in the join table from the names of the companies fixtures. if you've assigned the ids yourself, they won't match up with the calculated ones.

在companies.yml工作,您需要让rails自动为区域分配ID。这是因为(为了避免在companies.yml之前必须读取regions.yml),rails会根据公司灯具的名称计算它在连接表中的ID。如果您自己分配了ID,则它们将与计算出的ID不匹配。

From the sql you've provided it looks like you're setting the ids on the regions to 1 and 2, i.e. your regions.yml has

从您提供的sql看起来,您将区域上的ID设置为1和2,即您的regions.yml具有

one:
  id: 1
  name: MyString 

Remove the id:1 and you should be ok. You'll also need to update any other files (e.g. request_for_proposals.yml) that refer to regions, replacing

删除id:1,你应该没问题。您还需要更新引用区域的任何其他文件(例如request_for_proposals.yml)

region_id: 1

with

region: one

Rails will know that means to set region_id to the id for the region with label one in your fixtures.

Rails会知道这意味着将region_id设置为你的灯具中标签为1的区域的id。

#2


0  

Is your regions.yml in test/fixtures directory? How about companies.yml? Your code "regions: one, two", for a given company, should work.
Rails automatically loads the fixtures in this directory when you run the test. If the locations are correct, please post the test output - sql generated - when you run the tests.

你的regions.yml是test / fixtures目录吗? companies.yml怎么样?对于给定的公司,您的代码“区域:一,二”应该有效。运行测试时,Rails会自动加载此目录中的灯具。如果位置正确,请在运行测试时发布测试输出 - 生成的sql。

#3


0  

I'm unsure how does one use YAML fixtures these days. Have you tried FactoryGirl for creating db object instances while testing? It does pretty much the same as fixtures, in a much sophisticated way though.

我不确定这些天如何使用YAML灯具。您是否尝试过FactoryGirl在测试时创建数据库对象实例?它与固定装置几乎完全相同,但却非常复杂。

The following passages assumes you are using rspec as a testing framework.

以下段落假设您使用rspec作为测试框架。

After including factory_girl-rails in your Gemfile and updating spec/spec_helper.rb accordingly to the factory_girls README, create the following files:

在Gemfile中包含factory_girl-rails并相应地更新spec / spec_helper.rb到factory_girls自述文件后,创建以下文件:

# spec/factories/company_factories.rb
FactoryGirl.define do
  factory :company do
    title { |n| "Test Company #{n}" }

    # whatever else fields

    after_create { |company| company.regions << FactoryGirl.create(:region) }
  end
end

# spec/factories/region_factories.rb
FactoryGirl.define do
  factory :region do
    # whatever fields here
  end
end

# spec/factories/request_factories.rb
FactoryGirl.define do
  factory :request do        
    # whatever fields here
  end
end

Now the real question is - what's the actual test code you're doing?

现在真正的问题是 - 你正在做的实际测试代码是什么?

#4


0  

In addition to accepted answer if you have a need to calculate such id by yourself, e.g. you have some loose references or another datasource just add:

如果你需要自己计算这样的id,除了接受的答案,例如你有一些松散的引用或另一个数据源只是添加:

def fixh(key)
  ActiveRecord::FixtureSet.identify key
end

to your test_helper.rb and then use this way in fixtures:

到你的test_helper.rb,然后在灯具中使用这种方式:

security_context1:
  ext_id: <%= fixh :user1 %>