查找并销毁相关记录(Find and destroy dependent records)
在一段时间内,我的Rails应用程序有各种重写,在某些情况下,模型关联不正确。
目前我的
User
模型has_many :posts
及其destroy
方法正确地删除了所有依赖的Post
,但有时当事情写得不正确时,这并没有发生。 我现在留下了一些Post记录,这些记录会导致错误,因为他们的用户不存在。什么是最有效的方法来手动筛选所有Post记录,检查其用户是否确实存在,如果不破坏该帖子?
我想象的是:
Post.all.select{ |post| post.user.nil? }.destroy
但对于成千上万的记录而言,这似乎非常低效。 我很想知道最好的方法。 谢谢!
Over a period of time my Rails app has had various rewrites, and in some cases incorrect model associations.
Currently my
User
modelhas_many :posts
and itsdestroy
method correctly removes all dependentPost
s, but at times when things were improperly written this did not happen. I'm now left with a handful of Post records that cause errors all over because their User does not exist.What would be the most efficient way to manually filter through all Post records, check if its User actually exists, and if not destroy that Post?
I'm imagining something like:
Post.all.select{ |post| post.user.nil? }.destroy
But that seems incredibly inefficient for thousands of records. I'd love to know the best way to do this. Thank you!
原文:https://stackoverflow.com/questions/41512341
最满意答案
这不是一个jQuery错误 - 只是(又一个)浏览器的区别。
IE不会让你在选项元素上设置
display:none
( style.display ='none'不适用于选项标签 )。如果你在FF和IE中看你的小提琴 ,你会发现
<select>
仍然包含IE中的所有四个元素,但在FF中只有两个,无论jQuery存在。解决方案可能是实际删除元素并在需要时替换。
It's not a jQuery bug - just (yet another) browser difference.
IE won't let you set
display:none
on option elements (style.display='none' doesnt work on option tags).If you look at your fiddle in both FF and IE, you'll see that the
<select>
still contains all four elements in IE, but only two in FF, regardless of jQuery being present.The solution would probably be to actually remove the elements and replace when needed.
相关问答
更多-
我认为问题是那些filterUnits =“objectBoundingBox”后跟维度,你必须打算成为userSpace单位(37等)。 Chrome已经发现你必须指的是单位,但Firefox相信你知道你正在做什么,而你真的想让你的滤波器宽度达到边界框的3700%。 只需删除该声明,Firefox和Chrome开始看起来更像是彼此。 (照明单元在浏览器中也可能存在问题 - 但我认为这不是问题所在。) I think the problem is those filterUnits="objectBound ...
-
看起来jQuery的:visible选择器不适用于Chrome中的一些内联元素。 解决方案是添加一个显示样式,如"block"或"inline-block" ,使其工作。 还要注意的是,jQuery与许多开发人员的显示方式有所不同 如果元素在文档中占用空间,则元素被视为可见。 可见元素的宽度或高度大于零。 具有visibility: hidden元素visibility: hidden或opacity: 0被认为是可见的,因为它们仍然占用布局中的空间。 不在文档中的元素被认为是隐藏的; jQuery没有办法 ...
-
JavaScript .split函数在所有浏览器中都不起作用(JavaScript .split function does not work in all browsers)[2022-08-06]
split方法很好,它的trim导致了问题。 您可以使用MDN中的这个小填充 : if(!String.prototype.trim) { String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g,''); }; } String.prototype.trim在IE <9中不可用。上面的代码片段只是将split方法添加到String.prototype如果它尚不存在),并且行为与您期望的本机实现完全相同。 ... -
jQuery背景位置在FireFox中不起作用?(jQuery background position doesn't work in FireFox? [duplicate])[2021-07-24]
你可以随时创建自己的小插件,这并不难。 使用jQuery 1.8,我们现在可以访问$ .Animation方法,该方法直接为我们提供动画值,而不需要太多工作,所以我们可以执行如下操作: $.fn.animateBG = function(x, y, speed) { var pos = this.css('background-position').split(' '); this.x = parseInt(pos[0]) || 0; this.y = parseInt(pos[1] ... -
这是因为同源政策 。 你不能使用ajax来调用外部网站。 如果你真的想使用,你必须使用JSONP 。 或者您可以使用服务器端代理。 意味着,在服务器端调用外部站点并对该Web服务执行ajax调用。 更新: 在您的网站中创建webserveice,并在web方法中创建以下代码 string proxyURL = "http://devserver:7995/stdpart/services/GetAllPartsWithFilter"; HttpWebRequest request = (HttpWebReq ...
-
css在firefox中不起作用,但在所有其他浏览器中都可以(css doesn't work in firefox, but in all other browser it does)[2023-07-23]
缺少doctype ,尝试在标记之前添加 the doctype is missing, try adding before the tag -
jQuery“visible”在所有浏览器中都不起作用,但在Firefox中不起作用(jQuery “visible” doesn't work in all browsers, but in Firefox)[2022-05-30]
这不是一个jQuery错误 - 只是(又一个)浏览器的区别。 IE不会让你在选项元素上设置display:none ( style.display ='none'不适用于选项标签 )。 如果你在FF和IE中看你的小提琴 ,你会发现 -
您在点击处理程序的匿名函数中缺少event 。 $("#element a").click(function() { // ^^ You're missing event in the anonymous function of your click handler. $("#element a").click(function() { // ^^
-
在告诉浏览器启动 ajax请求后,您立即重新加载页面。 这可以防止发送请求,或者如果正在进行则中止请求。 它还会导致加载新页面和删除(如果服务器已收到)之间发生争用。 您根本不需要reload ,因为您正在动态删除该行。 只需删除 window.location.reload(); 完全排队。 但是如果你想要调用它,那么在请求完成之后再调用它: function delete_row(id) { if (confirm('Confirm to delete this Equipement ?')) ...
-
JPlayer在Firefox Android和其他所有浏览器上都不起作用(JPlayer Does Not Work On Firefox Android But All Other Browsers)[2022-09-09]
看起来jPlayer没有正确检测到Firefox for Android支持MP3播放。 修改所有出现的字符串'audio / mpeg; jPlayer库中的codecs =“mp3”'到'audio / mpeg'使得回放在Firefox for Android中正常运行。 但是,我不确定这是否会在任何其他浏览器中产生检测问题。 希望jPlayer团队可以在下一个版本中修复此检测。 It looks like jPlayer doesn't correctly detect that Firefox f ...