为什么Time.current不等于其解析的等价物?
current = Time.current
# Wed, 16 Sep 2015 17:10:56 CEST +02:00
parsed = Time.zone.parse('16 Sep 2015 17:10:56')
# Wed, 16 Sep 2015 17:10:56 CEST +02:00
current == parsed
# false <= What ?!
current.to_i == parsed.to_i
# true
Ticket.create(datetime: current)
# ...
Ticket.find_by_datetime(parsed)
# nil <= Why ?!
Run Code Online (Sandbox Code Playgroud)
我实际上在Ruby on Rails应用程序中遇到了麻烦,我尝试根据已解析的日期时间属性查找记录,如最后一行所示.
我真的不明白.时区是相同的,时间是相同的秒到秒.这里发生了什么事?
此外,我应该如何根据解析的日期时间查找记录?
我偶然发现了一篇关于Rails 3+范围的精彩文章:http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly -named-范围/ index.html中
你可以在那里阅读(在'Crazy Town'部分),可以合并来自不同模型的范围,如下所示:
class User < ActiveRecord::Base
scope :published, lambda {
joins(:posts).group("users.id") & Post.published
}
end
Run Code Online (Sandbox Code Playgroud)
它按预期工作,并允许您这样做:
User.published.to_sql
#=> SELECT users.* FROM "users"
# INNER JOIN "posts" ON "posts"."author_id" = "users"."id"
# WHERE (posts.published_at IS NOT NULL AND posts.published_at <= '2010-02-27 02:55:45.063181')
# GROUP BY users.id
Run Code Online (Sandbox Code Playgroud)
我在我的Rails 3.1项目中尝试了这种方法,显然它已不再适用了.
所以我克隆了文章的Rails 3.0.0-beta1项目,看到我的眼睛说这些人没有说谎,事情按照他们说的方式工作.
然后我3.1了,现在我得到了:
ruby-1.9.2-p290 :003 > User.published.to_sql
User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."author_id" = "users"."id" GROUP BY users.id
Post Load …Run Code Online (Sandbox Code Playgroud) 当我发送电子邮件时,我得到了这个奇怪的错误:
Failure/Error: publication = FactoryGirl.create(:publication, :author => author)
NoMethodError:
undefined method `index' for 2011-09-09 22:15:28 +0200:Time
Run Code Online (Sandbox Code Playgroud)
并且堆栈跟踪没有任何帮助.
任何的想法 ?
我正在构建一个React应用程序,可以将一个Person添加到ScheduleCell.在添加了Person之后,他可以确认或拒绝他的存在,这将触发CSS动画,以显示正在观看计划的人刚刚发生的事情.
一切都工作得很好,除了当我第一次加载时间表时,如果他已经确认或拒绝了提案,那么每个人都会动画.
由于图片胜过千言万语,这是我的问题:

有没有办法我可以检测到Person刚刚第一次呈现,所以我不添加触发CSS动画的CSS类,并且能够在下次添加该类?
这里有一些(咖啡)代码更清晰:
Person = React.createClass
render: ->
confirmation = @props.person.confirmation
className = 'alert '
if confirmation == undefined
className += 'neutral'
else if confirmation == true
className += 'good '
className += 'hvr-wobble-vertical' if @animate
else if confirmation == false
className += 'bad '
className += 'hvr-wobble-horizontal' if @animate
<div className={className}
{@props.person.username}
</div>
Run Code Online (Sandbox Code Playgroud)
我在这里想要的是@animate在首次呈现组件时(页面加载时)返回false,然后在呈现后一直返回true.
我试过这样做,但它似乎不起作用:
getInitialState: ->
{ mounted: false }
componentDidMount: ->
@setState { mounted: true }
animate: ->
@state.mounted
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间,
干杯!
我想做这个
apples, bananas = sort_basket
Run Code Online (Sandbox Code Playgroud)
所以我做了这个方法
def sort_basket
apples = []
bananas = []
basket.each do |fruit|
if fruit.apple?
apples << apple
else
bananas << banana
end
end
[apples, bananas]
end
Run Code Online (Sandbox Code Playgroud)
但我不禁觉得必须有更优雅和/或更简洁的方法来实现相同的行为sort_basket.
你会怎么做?
ruby ×2
css ×1
datetime ×1
deprecated ×1
javascript ×1
parsing ×1
reactjs ×1
refactoring ×1
scopes ×1