如果存在如何拒绝?对于非嵌套属性?

发布于 2024-08-27 09:22:17 字数 926 浏览 9 评论 0原文

目前,我的控制器允许用户一次提交多个“链接”。它将它们收集到一个数组中,为该用户创建它们,但捕获任何错误供用户返回并修复。如何忽略该用户已存在的任何链接的创建?我知道我可以将 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 技术交流群。

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

发布评论

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

评论(2

十六岁半 2024-09-03 09:22:17

试试这个:

@links = current_user.links.create(params[:links].reject{ |link| 
           current_user.links.exists?(:url=>link[:url]) })

或者,您可以在 Link 模型中为 url 属性添加唯一性检查。

class Link
  validates_uniqueness_of :url, :scope => [:user_id]
end

在您的控制器中:

@links = current_user.links.create(params[:links])

返回的结果集是新创建的 Link 对象的数组。任何与现有链接匹配的链接都将被忽略。

编辑

这是一次性完成此操作的另一种方法。

@links = params[:links].map{|link| 
           !current_user.links.exists?(:url=> link[:url]) and
             current_user.links.create(link)}.select{|link| link and 
               link.errors.empty?}

我仍然认为您应该让您的唯一验证工作并随后使用此代码:

@links = current_user.links.create(params[:links]).select{|link| 
           link.errors.empty?}

在后一种方法中,唯一性验证是在模型中完成的。这可以确保链接 url 的唯一性,无论链接是如何创建的。

Try this:

@links = current_user.links.create(params[:links].reject{ |link| 
           current_user.links.exists?(:url=>link[:url]) })

Alternatively you can add an uniqueness check in the Link model for the url attribute.

class Link
  validates_uniqueness_of :url, :scope => [:user_id]
end

In your controller:

@links = current_user.links.create(params[:links])

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.

@links = params[:links].map{|link| 
           !current_user.links.exists?(:url=> link[:url]) and
             current_user.links.create(link)}.select{|link| link and 
               link.errors.empty?}

I still think you should get your unique validation working and use this code afterwards:

@links = current_user.links.create(params[:links]).select{|link| 
           link.errors.empty?}

In the latter approach uniqueness validation is done in the model. This ensures link url uniqueness regardless how the link is created.

或十年 2024-09-03 09:22:17

在创建现有链接之前拒绝它们:

new_links = params[:links].reject{ |link| current_user.links.exists?(link) }

像这样思考。不确定这个代码...

Reject the existent links before create them:

new_links = params[:links].reject{ |link| current_user.links.exists?(link) }

Somethink like this. Not sure about this code...

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