简体   繁体   English

我可以在 Rails 中的其他设备之前加载一些设备以在连接表中循环它们吗?

[英]Can I load some fixtures before others in Rails to loop through them in a join table?

My Rails (4.2.11.1) application has a User model and a Role model.我的 Rails (4.2.11.1) 应用程序有一个User model 和一个Role model。 In my fixtures, I have a users_roles.yml file that I would like to populate by looping through all the fixtures in roles.yml , and assign them to a specific user.在我的装置中,我有一个users_roles.yml文件,我想通过遍历roles.yml中的所有装置来填充该文件,并将它们分配给特定用户。 I'm doing this with the block below:我正在使用以下块执行此操作:

users_roles.yml users_roles.yml

<% Role.all.each do |role| %>
<%= 'super_test_user' + role.name %>:
  user_id: <%= ActiveRecord::FixtureSet.identify(:super_test_user) %>
  role_id: <%= ActiveRecord::FixtureSet.identify(role.name.to_sym) %>
<% end %>

My issue is that when I run RAILS_ENV=test rake db:fixtures:load , Rails hits the users_roles.yml file first, and so there is nothing in the Role object for it to loop through.我的问题是,当我运行RAILS_ENV=test rake db:fixtures:load时,Rails 首先点击users_roles.yml文件,因此Role object 中没有任何内容可以循环。 I've got hundreds of roles, so want to avoid doing this manually if possible.我有数百个角色,因此希望尽可能避免手动执行此操作。

Is there a way that I can force Rails to load the roles.yml fixtures before the user_roles.yml file when I call RAILS_ENV=test rake db:fixtures:load ?当我调用RAILS_ENV=test rake db:fixtures:load时,有没有一种方法可以强制 Rails 在user_roles.yml文件之前加载roles.yml固定装置? I've tried the answer at Loading Rails Fixtures in a Specific Order when Testing , but can't get this to work unfortunately.我已经在Loading Rails Fixtures in a specific Order when Testing尝试了答案,但不幸的是无法让它工作。 I've adapted this answer in the following way in my test_helper.rb file:我在我的test_helper.rb文件中以下列方式调整了这个答案:

test_helper.rb test_helper.rb

class ActiveRecord::FixtureSet
  class << self
    alias :orig_create_fixtures :create_fixtures
  end

  def self.create_fixtures f_dir, fs_names, *args
    Role.delete_all
    User.delete_all

    reset_cache

    fs_names = %w(role user) & fs_names | fs_names

    orig_create_fixtures f_dir, fs_names, *args
  end
end

Have I done something wrong in the above?我在上面做错了吗? Or is there another way I could achieve this?还是有另一种方法可以实现这一目标?

EDIT: I should also add the reason I am loading fixtures in such a way is because this Rails application (let's call it 'A') is being called from another Rails application (B).编辑:我还应该添加我以这种方式加载固定装置的原因是因为这个 Rails 应用程序(我们称之为“A”)是从另一个 Rails 应用程序(B)调用的。 As such, the test fixtures in 'A' need to be loaded into the test database before application 'B' calls it in the test environment.因此,在应用程序“B”在测试环境中调用它之前,需要将“A”中的测试夹具加载到测试数据库中。

OK - so I've found a solution that's a bit hacky but it works.好的 - 所以我找到了一个有点 hacky 但它有效的解决方案。 I'll post here in case anyone else runs into the same issue:我会在这里发布以防其他人遇到同样的问题:

  1. Run rake db:test:prepare运行rake db:test:prepare
  2. Run RAILS_ENV=test rake db:fixtures:load FIXTURES=roles运行RAILS_ENV=test rake db:fixtures:load FIXTURES=roles
  3. Now run RAILS_ENV=test rake db:fixtures:load现在运行RAILS_ENV=test rake db:fixtures:load

This ensures all the roles have been pre-loaded into the correct db table and can be referenced from the Role object in the join table fixture file.这确保所有角色都已预加载到正确的数据库表中,并且可以从连接表夹具文件中的Role object 中引用。

I'd prefer to have this automated in the test_helper.rb file though.不过,我更希望在test_helper.rb文件中自动执行此操作。 Any ideas how I could do this?任何想法我怎么能做到这一点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails和db:fixtures:load - 它可以忽略一些模型吗? - Ruby on Rails and db:fixtures:load - can it ignore some models? 在加载课程之前加载导轨固定装置 - Load rails fixtures before classes are loaded rails 如何在没有 rspec 的测试之前加载测试夹具? - rails How to load test fixtures before tests without rspec? 使用Rails,如何将联接表与联接表联接? - Using Rails, How can I join a join table with a join table? 在javascript认为文档“准备好”之前,如何让我的茉莉花测试装置加载? - How can I get my jasmine tests fixtures to load before the javascript considers the document to be “ready”? rails 通过连接表预先加载 has_many - rails eager load has_many through join table 在Rails中,每个表如何使用多套夹具? - How can I use more than one set of fixtures per table in Rails? 如何在Rails中通过:through关联的联接表设置条件的查找? - How can I do a find in Rails with conditions set on the join table of a :through association? 我可以加入没有关联的表吗? (rails / sql) - Can I join a table with no association? (rails/sql) Rails无法从具有HABTM关系的夹具中加载数据 - Rails, can't load data from fixtures that have HABTM relationshionship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM