[英]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:我会在这里发布以防其他人遇到同样的问题:
rake db:test:prepare
运行rake db:test:prepare
RAILS_ENV=test rake db:fixtures:load FIXTURES=roles
运行RAILS_ENV=test rake db:fixtures:load FIXTURES=roles
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.