首页 \ 问答 \ Rails:在停止之前验证该开始,DateTime(Rails: Validating that start is before stop, DateTime)

Rails:在停止之前验证该开始,DateTime(Rails: Validating that start is before stop, DateTime)

我正在尝试使用validates_timeliness来确保SliderImage.start总是在SliderImage.stop之前:

class SliderImage < ActiveRecord::Base

  validates_datetime :start, :stop
  validates :start, :timeliness => {:before => stop}

end

但当然, stop还没有定义。 我怎样才能做到这一点?


I am trying to use validates_timeliness to ensure that SliderImage.start is always before SliderImage.stop:

class SliderImage < ActiveRecord::Base

  validates_datetime :start, :stop
  validates :start, :timeliness => {:before => stop}

end

But of course stop is not defined yet. How can I accomplish this?


原文:https://stackoverflow.com/questions/8853983
更新时间:2021-12-18 06:12

最满意答案

如果您想要最可维护的选项,则只提供一个async API,它不会执行任何阻塞调用或使用任何线程池线程来实现。

如果您真的想同时具有async和同步API,那么您将遇到可维护性问题。 你真的需要实现两次:一次async ,一次同步。 这两种方法看起来几乎相同,所以初始化实现很容易,但是最终会有两种分开的几乎相同的方法,所以维护是有问题的。

特别是,没有一个很好的简单的方法来做一个async或同步的“包装”。 Stephen Toub有关于该主题的最佳信息:

  1. 我应该为同步方法公开异步包装器吗?
  2. 我应该为异步方法公开同步包装器吗?

(两个问题的简短答案是“否”)


If you want the most maintainable option, only provide an async API, which is implemented without making any blocking calls or using any thread pool threads.

If you really want to have both async and synchronous APIs, then you'll encounter a maintainability problem. You really need to implement it twice: once async and once synchronous. Both of those methods will look nearly identical so the initial implementation is easy, but you will end up with two separate nearly-identical methods so maintenance is problematic.

In particular, there's no good and simple way to just make an async or synchronous "wrapper". Stephen Toub has the best info on the subject:

  1. Should I expose asynchronous wrappers for synchronous methods?
  2. Should I expose synchronous wrappers for asynchronous methods?

(the short answer to both questions is "no")

相关问答

更多
  • 您可以将它包装在Task.Run() ,如下所示,如果您希望它被异步调用: public override bool TestMethod() { var task = Task.Run(async () => { return await engine.DoAsyncFunction(); }); var Result = task.Result; // use returned result from async method here } You can w ...
  • 如果您想要最可维护的选项,则只提供一个async API,它不会执行任何阻塞调用或使用任何线程池线程来实现。 如果您真的想同时具有async和同步API,那么您将遇到可维护性问题。 你真的需要实现两次:一次async ,一次同步。 这两种方法看起来几乎相同,所以初始化实现很容易,但是最终会有两种分开的几乎相同的方法,所以维护是有问题的。 特别是,没有一个很好的简单的方法来做一个async或同步的“包装”。 Stephen Toub有关于该主题的最佳信息: 我应该为同步方法公开异步包装器吗? 我应该为异步方法 ...
  • 此外,突然CPU绑定计算变得异步,这不是它应该如何工作... 这背后有一个简单的原因:当人们看到异步方法时,他们通常认为,这个方法将异步执行,因此不会阻塞当前线程。 你的方法会这样: //Start... (On current thread) //Access the database (On IO Thread, freeing up the current thread) //Return to current thread //Do the calculations (O ...
  • 回答我自己的问题: require 'em-hiredis' require 'sinatra/base' class App < Sinatra::Base def redis @redis ||= EM::Hiredis.connect end get '/' do stream :keep_open do |out| redis.blpop('abcdef', 15).callback do |x| out << "x=#{x}" ...
  • 如果你的库需要实现同步和异步成员,那么你实现了两个成员。 没有捷径(假设这是一个可重用的库)。 public async Task GetContentAsync(string url) { ... // Logic here, e.g., using HttpClient } public string GetContent(string url) { ... // Duplicate logic here, e.g., using WebClient } 逻辑的重复当然是不幸 ...
  • 如果您使用的是Web API,如果该方法不是真正的 async则不会使用async Log方法。 看看Stephen Cleary的这个答案。 If you're using Web API I would not go for an async Log method if the method is not truly async. Have a look at this answer from Stephen Cleary.
  • 在引擎盖下播放使用基于NIO的async-http-client 。 将调度请求,并且当服务器响应时,将在来自线程池的线程上执行回调(在这种情况下,将来完成)。 这样就不会阻塞任何线程。 Under the hood play uses async-http-client which is based on NIO. A request will be dispatched and when the server responds, a callback (in this case the completi ...
  • 我的问题是我希望同步函数能够等待来自异步的值... 他们不能,因为: JavaScript基于由线程处理的“作业队列”,其中作业具有运行到完成语义,并且 JavaScript实际上并没有异步功能(真的 - 坚持这个......) 作业队列(事件循环)在概念上非常简单:当需要完成某些事情(初始执行脚本,事件处理程序回调等)时,该工作将被放入作业队列中。 为该作业队列提供服务的线程获取下一个待处理作业,将其运行至完成,然后返回下一个作业。 (当然,它比这更复杂,但这足以满足我们的目的。)因此,当一个函数被调用时 ...
  • 正如@loteq所提到的,你可以简单地在Future上调用.actionGet()来使它同步。 检查elasticsearch集成测试 ,他们有一些非常好的例子。 As @loteq mentioned, you can simply call .actionGet() on a Future to make it synchronous. Check elasticsearch integration tests, they have some very good examples.
  • 根据OP中提供的链接文档,访问API应该在您的预期应用程序中看起来像这样。 public async Task> TestAsync() { var client = new InfluxClient(new Uri("http://localhost:8086")); var users = await client.ShowUsersAsync(); return users; } 虽然OP中的简单示例使用控制台应用程序,但这里假设控 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)