开发者

Rails finding notes by date

开发者 https://www.devze.com 2023-01-06 22:51 出处:网络
I\'ve created a simple rails app that has a people and a notes model. People have many notes and notes belong to people

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

开发者_JS百科 #

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


To get all the records for any given date:

@person.notes.all( :conditions=> ["created_at BETWEEN ? AND ? ", 
    date.beginning_of_day, date.end_of_day] )

To get the first record on any given date:

@person.notes.first( :conditions=> ["created_at BETWEEN ? AND ? ", 
    date.beginning_of_day, date.end_of_day], :order => :created_at )

To get all the records for today (assuming created_at is always less than the current time):

@person.notes.all( :conditions=> ["created_at >= ? ", 
    Time.now.beginning_of_day] )

To get the first record for today (assuming created_at is always less than the current time):

@person.notes.first( :conditions=> ["created_at >= ? ", 
    Time.now.beginning_of_day], :order => :created_at )


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|.

0

精彩评论

暂无评论...
验证码 换一张
取 消