views:

38

answers:

2

Hello

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

+2  A: 

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)
KandadaBoggu
Thank you for your reply.Your syntax works perfectly and produces the right sql statement (I ran it through mysql to check) but for some reason my app refuses to show the notes.My show view looks like this.## I added the code to the original post ##I think I'm doing something wrong in my view...
Toggo
Have you checked to ensure you have data for the person? Try running the command in `script\console` to see if there is data.
KandadaBoggu
+1  A: 

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

theIV
Thank you. That worked.
Toggo