1

I have a system where users can check-in/check-out books, I want to make it so a user can keep a book for one week.

How can I do 'created_at + 1 week' for my book loan item?

At the moment I have a table like this which displays some information: - On the 'check-in time' column I would like to add check-out time + 7 days

<table>
  <tr>
    <th>Author</th>
    <th>Title</th>
    <th>User Name</th>
    <th>User Email</th>
    <th>Check-Out time</th>
    <th>Check-In time</th>
  </tr>

<% for item in @books_loaned %>
  <tr>
    <td><%= item.book.author %></td>
    <td><%= item.book.title %></td>
    <td><%= item.user.name %></td>
    <td><%= item.user.email %></td> 
    <td><%= item.created_at %></td>
    <td><%= button_to "Check-In", {:controller => :librarian, :action =>                                                                          
    :check_in}, {:method => :post} %></td>    
  </tr>
<% end %>
</table>
Mark Shakespeare
  • 115
  • 1
  • 3
  • 9
  • possible duplicate of [How to add 10 days to current time in rails?](http://stackoverflow.com/questions/4654457/how-to-add-10-days-to-current-time-in-rails) – lcguida May 04 '15 at 20:22

2 Answers2

2

there are many possible ways to do this:

If you want to display only Date not the time then:

<%= item.created_at.to_date.next_week %> 
#this will add 6 days and output will be seventh day

or if you want to add days manually

<%= item.created_at.to_date+7.days %>

or

<%= item.created_at.to_date+1.week %>

if you also want to display time then remove .to_date from above code

Abdul Baig
  • 3,683
  • 3
  • 21
  • 48
1

Simple add 7.days

In your example:

<table>
  <tr>
    <th>Author</th>
    <th>Title</th>
    <th>User Name</th>
    <th>User Email</th>
    <th>Check-Out time</th>
    <th>Check-In time</th>
  </tr>

<% for item in @books_loaned %>
  <tr>
    <td><%= item.book.author %></td>
    <td><%= item.book.title %></td>
    <td><%= item.user.name %></td>
    <td><%= item.user.email %></td> 
    <td><%= item.created_at + 7.days %></td>
    <td><%= button_to "Check-In", {:controller => :librarian, :action =>                                                                          
    :check_in}, {:method => :post} %></td>    
  </tr>
<% end %>
</table>

also possible 1.week apparently. All available in Rails through ActiveSupport: http://edgeguides.rubyonrails.org/active_support_core_extensions.html.

Related question: How to add 10 days to current time in Rails

Community
  • 1
  • 1
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98