是否应该在一行上验证多个属性?

发布于 2024-12-08 05:44:17 字数 403 浏览 0 评论 0原文

使用我使用的较旧的 Shoulda 插件,我曾经能够执行如下操作:

should_have_many :posts, :authors, :comments

升级我的 Shoulda 版本后,我必须将其更改为如下所示:

should have_many :posts
should have_many :authors
should have_many :comments

我尝试将其全部放在一行上,所以它是只是,

should have_many :posts, :authors, :comments

但这不起作用。有没有办法把它弄干一点?

我正在使用应该2.11.3

With the older Shoulda plugin I was using, I used to be able to do something like the following:

should_have_many :posts, :authors, :comments

After upgrading my version of Shoulda, I had to change it to something like this:

should have_many :posts
should have_many :authors
should have_many :comments

I've tried putting it all on one line so it's just

should have_many :posts, :authors, :comments

but that's not working. Is there anyway to DRY this up a little?

I'm using Shoulda 2.11.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2024-12-15 05:44:17

您可以尝试:

[:posts, :authors, :comments].each do |models|
  it { should have_many(models) }
end

这将作为三个单独的测试显示在结果中。

我使用 Rspec,所以上面是 Rspec 格式,但我认为类似的方法应该适用于 Test::Unit。

我不确定这是否能为你节省那么多,但如果你有很多相同类型的关系,它会变得更有价值。也许它对 allow_mass_assignment_of 更有用。

You could try:

[:posts, :authors, :comments].each do |models|
  it { should have_many(models) }
end

This will show up in the results as three separate tests.

I use Rspec, so above is Rspec formatting, but I assume a similar approach should work with Test::Unit.

I'm not sure if this is going to save you all that much but if you have many of the same type of relationship it would become more valuable. Perhaps it is of better use for allow_mass_assignment_of.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文