轨道按日期查找笔记 [英] Rails finding notes by date
问题描述
我创建了一个简单的rails应用程序,它有一个人和一个笔记模型。
人们有许多笔记和注释属于人们
该应用程序的工作原理,我可以显示特定人物的所有笔记,但我希望能够在特定日期显示一个音符。
默认情况是今天的日期。
我目前有以下:
<%@ person.notes.each do | note | %>
< p>
< h3><%= h note.title%>< / h3>
< / p>
< p>< b>正文:< / b>< br />
<%= h note.body%>
我尝试了几种不同的方法,但无法让我向我显示今天的日期
我尝试创建一个命名范围,并失败,并尝试使用:
@ person.notes.find_by_created_at(Time.now-1.day)
任何人都可以指向正确的方向吗?
谢谢
Tom
#
我使用下面的代码,并提出了这个。
我的展示视图看起来像这样。
< p>
< h1><%= h @ person.name%>< / h1>
< / p>
< h2>注意< / h2>
<%@ person.notes.all(:conditions => [created_at> =?,Time.now.beginning_of_day])do | note | %>
< p>
< h3><%= h note.title%>< / h3>
< / p>
< p>< b>正文:< / b>< br />
<%= h note.body%>
< / p>
<%end%>
我认为我可能在我看来使用错误的语法,或者甚至不应该使用在视图中?
T
根据您更新的问题,您需要迭代查询中返回的项目。
<%@ person.notes.all( conditions => [created_at> =?,Time.now.beginning_of_day])。 %>
请注意,该查询确实属于某个模型,并从控制器调用; p>
编辑:既然我真的不觉得我回答了你的原始问题,我会补充一点我的答案。
你可以把你的注释
模型中的查询作为命名范围。以下行:
named_scope:今天,lambda {| date | {:conditions => [created_at> =?,Time.now.beginning_of_day]}}
t测试,因为我不记得确切的语法,但是您需要使用lambda或其他的,当您的rails应用程序第一次启动时,它将插入今天的日期在那里,永远不会当对named_scope进行后续请求时更新。有关此轨道通讯的上述情况的更多信息。
在您的 PeopleController
中,您可以将其作为 @todays_notes = @ person.notes.today
然后在您的视图中使用 @ todays_notes.each do | note |
。
I've created a simple rails app that has a people and a notes model.
People have many notes and notes belong to people
The app works and I'm able to show all the notes for a specific person, but I'd like to be able to show a single note for a specific date.
The default scenario being todays date.
I currently have the following:
<% @person.notes.each do |note| %>
<p>
<h3><%=h note.title %></h3>
</p>
<p><b>Body: </b><br />
<%=h note.body %>
I have tried several different methods but can't get any to show me a note for the date today.
I tried to create a named scope and failed with that and also tried to use:
@person.notes.find_by_created_at(Time.now-1.day)
Can anyone point me in the right direction please?
Thanks
Tom
#I used the code from below and came up with this.
My show view looks like this.
<p>
<h1><%=h @person.name %></h1>
</p>
<h2>Notes</h2>
<% @person.notes.all(:conditions=> ["created_at >= ? ", Time.now.beginning_of_day]) do |note| %>
<p>
<h3><%=h note.title %></h3>
</p>
<p><b>Body: </b><br />
<%=h note.body %>
</p>
<% end %>
I think I may be using the wrong syntax in my view, or should possibly not even be using this in the view?
T
Based on your updated question, you need to iterate over the items that are returned from your query.
<% @person.notes.all(:conditions=> ["created_at >= ? ", Time.now.beginning_of_day]).each do |note| %>
Mind you, that query really does belong in a model somewhere and called from a controller ;)
Edit: Since I don't really feel like I answered your original question, I'll add to my answer a little bit.
You could definitely put that query in your Note
model as a named scope. Something along the lines of:
named_scope :today, lambda { |date| { :conditions => ["created_at >= ? ", Time.now.beginning_of_day] } }
The above isn't tested as I don't remember the exact syntax, but you'll need to use a lambda or else when your rails app starts for the first time, it will insert today's date in there and never be updated when subsequent requests to the named_scope are made. There's more information on the above situation in this railscast.
In your PeopleController
you could fetch them as @todays_notes = @person.notes.today
and then iterate over them in your view with @todays_notes.each do |note|
.
这篇关于轨道按日期查找笔记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!