如果存在如何拒绝?对于非嵌套属性?
目前,我的控制器允许用户一次提交多个“链接”。它将它们收集到一个数组中,为该用户创建它们,但捕获任何错误供用户返回并修复。如何忽略该用户已存在的任何链接的创建?我知道我可以将 validates_uniqueness_of 与该用户的范围一起使用,但我宁愿完全忽略他们的创建。这是我的控制器:
@链接= params[:links].values.collect{ |link| current_user.links.create(链接) }.拒绝{ |p| p.错误.空? }
链接都有一个 url,因此我考虑检查该用户是否已存在该 link.url,但不太确定如何或在哪里执行此操作。我应该以某种方式将其附加到我的控制器上吗?或者它应该是模型中的一个新方法,就像 before_validation 回调中那样? (注意:这些“链接”不是嵌套的,但它们确实属于:user。)
因此,如果可能的话,我希望能够忽略这些链接的创建。就像如果一个用户提交了 5 个链接,但其中 2 个已经存在,那么我只想忽略这 2 个,而创建其他 3 个。我该怎么做呢?
编辑:感谢 Kandada,我现在正在使用这个:
@链接= params[:links].values.collect.reject{ |链接| current_user.links.exists?(:url=>link[:url])}
@links = @links.collect{ |link| current_user.links.create(链接) }.拒绝{ |p| p.errors.empty? }
我将两者分开,首先检查是否存在,然后创建那些未被拒绝的。有没有更好的方法来做到这一点,比如组合这两个语句可能会提高性能?如果不是的话,我想我已经很满足了。 (再次感谢 Kandada 和 j。)
Currently my controller lets a user submit muliple "links" at a time. It collects them into an array, creates them for that user, but catches any errors for the User to go back and fix. How can I ignore the creation of any links that already exist for that user? I know that I can use validates_uniqueness_of with a scope for that user, but I'd rather just ignore their creation completely. Here's my controller:
@links =
params[:links].values.collect{ |link|
current_user.links.create(link)
}.reject { |p| p.errors.empty? }
Each link has a url, so I thought about checking if that link.url already exists for that user, but wasn't really sure how, or where, to do that. Should I tack this onto my controller somehow? Or should it be a new method in the model, like as in a before_validation Callback? (Note: these "links" are not nested, but they do belong_to :user.)
So, I'd like to just be able to ignore the creation of these links if possible. Like if a user submits 5 links, but 2 of them already exist for him, then I'd just like for those 2 to be ignored, while the other 3 are created. How should I go about doing this?
Edit: Thanks to Kandada, I'm now using this:
@links =
params[:links].values.collect.reject{
|link|
current_user.links.exists?(:url=>link[:url])}@links = @links.collect{ |link|
current_user.links.create(link)
}.reject { |p| p.errors.empty? }
So I separated the two, to first check if there are any that exist, then to create those that weren't rejected. Is there a better way to do this, like maybe combining the two statements would increase performance? If not, I think I'm pretty satisfied. (thank you again Kandada and j.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
或者,您可以在
Link
模型中为url
属性添加唯一性检查。在您的控制器中:
返回的结果集是新创建的
Link
对象的数组。任何与现有链接匹配的链接都将被忽略。编辑
这是一次性完成此操作的另一种方法。
我仍然认为您应该让您的唯一验证工作并随后使用此代码:
在后一种方法中,唯一性验证是在模型中完成的。这可以确保链接 url 的唯一性,无论链接是如何创建的。
Try this:
Alternatively you can add an uniqueness check in the
Link
model for theurl
attribute.In your controller:
The result set returned is an array of newly created
Link
objects. Any links matching the existing links are ignored.Edit
Here is another way to do this in one pass.
I still think you should get your unique validation working and use this code afterwards:
In the latter approach uniqueness validation is done in the model. This ensures link url uniqueness regardless how the link is created.
在创建现有链接之前拒绝它们:
像这样思考。不确定这个代码...
Reject the existent links before create them:
Somethink like this. Not sure about this code...