【问题标题】:Incorrect association in fixtures固定装置中的错误关联
【发布时间】:2023-04-04 20:07:01
【问题描述】:

我正在使用 Rails 4.1.1 遵循指南 http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures,并在 https://github.com/codetriage/codetriage 项目中相互使用命名装置。但是,当我尝试从另一个夹具引用一个夹具时,它不起作用:

# fixtures/issues.rb

issue_triage_sandbox_issue:
  id: 4
  comment_count:
  url: https://api.github.com/repos/bemurphy/issue_triage_sandbox/issues/1
  last_touched_at: 2012-11-10 22:20:24.000000000 Z
  number: 1
  created_at: 2012-11-10 23:23:45.281189000 Z
  updated_at: 2012-11-10 23:23:45.281189000 Z
  repo: issue_triage_sandbox
  title: first test issue in sinatra
  html_url: https://github.com/sinatra/sinatra/issues/1
  state: open

# fixtures/users.rb

issue_triage_sandbox:
  id: 1
  user_name: bemurphy
  name: issue_triage_sandbox
  full_name: bemurphy/issue_triage_sandbox
  language: ruby
  created_at: 2012-11-10 21:50:48.351554000 Z
  updated_at: 2012-11-10 21:50:48.351554000 Z
  issues_count: 1

您可以看到问题应该是加载 repo issue_triage_sandbox repo。但在我的测试中它不是:

issue = issues(:issue_triage_sandbox_issue)
puts issue.repo
# => nil

puts issue.inspect
#<Issue id: 4, comment_count: nil, url: "https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>     

任何想法为什么会通过引用不存在的存储库来创建问题?

【问题讨论】:

标签: ruby-on-rails ruby fixtures


【解决方案1】:

我认为它必须与 repos 夹具中的 id 属性一起使用。 我正在使用 yml 固定装置。我将问题和 repo 添加到项目中的现有问题中。

issues.yml

issue_triage_sandbox:
  user_name: bemurphy
  name: issue_triage_sandbox
  full_name: bemurphy/issue_triage_sandbox
  language: ruby
  created_at: 2012-11-10 21:50:48.351554000 Z
  updated_at: 2012-11-10 21:50:48.351554000 Z
  issues_count: 1

repos.yml

issue_triage_sandbox:
  user_name: bemurphy
  name: issue_triage_sandbox
  full_name: bemurphy/issue_triage_sandbox
  language: ruby
  created_at: 2012-11-10 21:50:48.351554000 Z
  updated_at: 2012-11-10 21:50:48.351554000 Z
  issues_count: 1

从控制台:

rake db:fixtures:load RAILS_ENV=test 轨道 c 测试

irb(main):001:0> i = Issue.last

=> #https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number :1,created_at:“2012-11-10 23:23:45”,updated_at:“2012-11-10 23:23:45”,repo_id:915227508,title:“sinatra 中的第一个测试问题”,html_url:“ https://github.com/sinatra/sinatra/issues/1",状态:"打开",pr_attached:false>

irb(main):002:0> i.repo

【讨论】:

【解决方案2】:

根据您项目here 中的代码,看起来Issues 属于Repos。我想知道固定装置在这种关系中是否有“倒退”的问题。

您可以尝试使用 ERB 来获取正确夹具的 ID 并像这样应用它: repo_id: <%= ActiveRecord::FixtureSet.identify(:issue_triage_sandbox) %>

【讨论】:

  • ops的问题是基于标签的引用被解析为记录的生成id,而op指定了他们要使用的id。您的识别解决方案也会返回此生成的 ID。
猜你喜欢
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多