在保留id的同时避免每个轨道中的空对象(Avoiding empty objects in rails each while preserving ids)
以下模板基于http://guides.rubyonrails.org/getting_started.html上的rails教程
我正在创建内联的“real_comments”以避免由@ article.comments.build形式创建的额外空注释,但是当我尝试删除它时,这会导致错误,因为注释ID丢失了。
No route matches [DELETE] "/articles/2/comments"
是否有可能删除构建创建的额外注释,同时维护用于对注释执行操作的索引? 我通过玩弄自己想出了real_comments解决方案,所以我毫不怀疑我正在做一些非常不受影响的问题,导致问题
<p> <strong>Title:</strong> <%= @article.title %> </p> <p> <strong>Text:</strong> <%= @article.text %> </p> <%#How to copy an array in ruby %> <% real_comments = @article.comments.map do |e| e.dup end %> <h2>Add a comment:</h2> <%= form_for([@article, @article.comments.build]) do |f| %> <p> <%= f.label :commenter %> <%= f.text_field :commenter %> </p> <p> <%= f.label :body %> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %> <h2>Comments:</h2> <% real_comments.each do |comment| %> <p> <strong>Commenter:</strong> <%= comment.commenter %> </p> <p> <strong>Comment:</strong> <%= comment.body %> </p> <%= link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: {confirm: 'Are you sure?'} %> <% end %> <%= link_to 'Edit', edit_article_path(@article) %> <%= link_to 'Back', articles_path %>
The following template is based off of the rails tutorial at http://guides.rubyonrails.org/getting_started.html
I'm creating the "real_comments" inline to avoid an extra empty comment created by the form @article.comments.build however this causes an error as the comment id is missing when I try to delete it.
No route matches [DELETE] "/articles/2/comments"
Is it possible to strip out the extra comment created by build while maintaining the index for performing operations on the comment by id? I came up with the real_comments solution myself by playing around so I have no doubt I'm doing something very un-rails which is causing the issue
<p> <strong>Title:</strong> <%= @article.title %> </p> <p> <strong>Text:</strong> <%= @article.text %> </p> <%#How to copy an array in ruby %> <% real_comments = @article.comments.map do |e| e.dup end %> <h2>Add a comment:</h2> <%= form_for([@article, @article.comments.build]) do |f| %> <p> <%= f.label :commenter %> <%= f.text_field :commenter %> </p> <p> <%= f.label :body %> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %> <h2>Comments:</h2> <% real_comments.each do |comment| %> <p> <strong>Commenter:</strong> <%= comment.commenter %> </p> <p> <strong>Comment:</strong> <%= comment.body %> </p> <%= link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: {confirm: 'Are you sure?'} %> <% end %> <%= link_to 'Edit', edit_article_path(@article) %> <%= link_to 'Back', articles_path %>
原文:https://stackoverflow.com/questions/41728920
满意答案
我有点像冒泡的方法。 我在进行递归操作时发现的问题是它们适用于小型集合(想想小于5或10k),然后当你变大时表现得很糟糕。 出于这个原因,我喜欢光标方法,你实际上是在说:“你是否大于标准?是,否。插入或忽略,删除,继续前进。” 这样,您只需对一次和每次一次的项目进行评估,而不是对递归主题的每个变体进行评估。
DECLARE @Temp TABLE ( id INT , dt DATETIME , txt VARCHAR(8) , rwn INT ) DECLARE @Holder TABLE ( id INT , dt DATETIME , txt VARCHAR(8) , Dif int ) INSERT INTO @Temp SELECT *, row_number() over (partition by txt order by dt, id) AS rn From wt WHILE EXISTS (SELECT 1 FROM @Temp) BEGIN DECLARE @CurId INT , @CurDt DATETIME , @Curtxt VARCHAR(8) , @LastDate DATETIME ; SELECT TOP 1 @CurId = Id, @CurDt = Dt, @Curtxt = txt FROM @Temp ORDER BY txt, rwn --If there is not entry you need a single entry IF NOT EXISTS (SELECT TOP 1 * FROM @Holder) BEGIN INSERT INTO @Holder VALUES (@CurId, @CurDt, @curtxt, null) END ELSE --if you reset the grouping you need to reset and begin anew IF (SELECT rwn FROM @Temp WHERE Id = @CurId) = 1 BEGIN INSERT INTO @Holder VALUES (@CurId, @CurDt, @curtxt, null) END --if you are going along check the logic for the difference of what the last was compared to the current ELSE BEGIN SELECT TOP 1 @LastDate = dt FROM @Holder ORDER BY id desc IF DATEDIFF(HOUR, @LastDate, @CurDt) >= 1 BEGIN INSERT INTO @Holder VALUES (@CurId, @CurDt, @curtxt, DATEDIFF(MINUTE, @LastDate, @CurDt)) END END --Delete the running values and loop again DELETE @Temp WHERE Id = @CurId END Select * From @Holder
I kind of like a method that is a bubble sort. The problem I have found when doing recursive operations is they work great for small sets(think less than 5 or 10k), then behave horrid when you get larger. For this reason I like a cursor approach were you are essentially saying: "Are you larger than a criteria? Yes, No. Insert or Ignore, Delete, move on." This way you are evaluating over every item once and once only, not every variation of a theme of recursion.
DECLARE @Temp TABLE ( id INT , dt DATETIME , txt VARCHAR(8) , rwn INT ) DECLARE @Holder TABLE ( id INT , dt DATETIME , txt VARCHAR(8) , Dif int ) INSERT INTO @Temp SELECT *, row_number() over (partition by txt order by dt, id) AS rn From wt WHILE EXISTS (SELECT 1 FROM @Temp) BEGIN DECLARE @CurId INT , @CurDt DATETIME , @Curtxt VARCHAR(8) , @LastDate DATETIME ; SELECT TOP 1 @CurId = Id, @CurDt = Dt, @Curtxt = txt FROM @Temp ORDER BY txt, rwn --If there is not entry you need a single entry IF NOT EXISTS (SELECT TOP 1 * FROM @Holder) BEGIN INSERT INTO @Holder VALUES (@CurId, @CurDt, @curtxt, null) END ELSE --if you reset the grouping you need to reset and begin anew IF (SELECT rwn FROM @Temp WHERE Id = @CurId) = 1 BEGIN INSERT INTO @Holder VALUES (@CurId, @CurDt, @curtxt, null) END --if you are going along check the logic for the difference of what the last was compared to the current ELSE BEGIN SELECT TOP 1 @LastDate = dt FROM @Holder ORDER BY id desc IF DATEDIFF(HOUR, @LastDate, @CurDt) >= 1 BEGIN INSERT INTO @Holder VALUES (@CurId, @CurDt, @curtxt, DATEDIFF(MINUTE, @LastDate, @CurDt)) END END --Delete the running values and loop again DELETE @Temp WHERE Id = @CurId END Select * From @Holder
相关问答
更多Rx.Observable.interval,每个事件的间隔不同(Rx.Observable.interval with different interval for each event)
间隔函数在oracle中(interval function in oracle)
1小时后如何选择行?(How to select rows be create after 1 hour?)
选择一个间隔的几个平均值(select several averages of an interval)
mysql,查询间隔错误(mysql, error in query interval)
phpMyAdmin事件错误#1542间隔太大(phpMyAdmin event error #1542 interval too big)
选择按HOUR()分组的时间序列(Select time series grouped by HOUR() with wrap around)
Postgresql,选择输出结果+1小时(Postgresql, output result from select with +1 hour)
Zenoss MySQL查询 - 每小时事件(Zenoss MySQL Query - Events per hour)
查询以至少一个小时的间隔选择相同的事件代码(Query to select same event code with at least one hour interval)
相关文章
更多Guava Objects类详解
Rails常用插件
rails页面文件html.erb如何访问ActiveRecord的资源
Rails中的路由功能是如何对应的?
Rails 风格指导
rails save问题数据库表主键必须是id吗?
Rails设置环境变量
在Rails中如何打开一个外部URL,并得到该URL的返回结果。
請問:Rails該如何存取SQL2000中的圖像字段
如何在javascript中写rails的helper代码
最新问答
更多python的访问器方法有哪些
使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
透明度错误IE11(Transparency bug IE11)
linux的基本操作命令。。。
响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
元素上的盒子阴影行为(box-shadow behaviour on elements)
它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640
Copyright ©2023
peixunduo.com All
Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!