I'm querying orders by date range. It is working now but I have the logic in the controller not in the model. I know that this should be in the model instead. I've tried several approaches but I've had no luck.
Question: What is the proper way of doing this?
I'm looking at the Rails Guides Active Record Querying section 2.2 Array Conditions: http://guides.rubyonrails.org/active_record_querying.html
Also researched several question here in Stack overflow: undefined local variable or method `params' for #<Result:0x3904b18>
undefined local variable or method `user_params' rails 4
ruby query between two date parameters
orders.rb Empty now to avoid errors
def self.search_range
end
orders_controller.rb
def search_range
@orders = Order.where("created_at >= :start_date AND created_at <= :end_date",{start_date: params[:start_date], end_date: params[:end_date]}).order("created_at desc")
end
search_range.html.erb Here I'm entering the date range
<div class="container-fluid events-container">
<div class="row">
<div class="col-sm-12">
<h1>Orders</h1>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<%= form_tag search_range_path, :method => 'get', class:"" do %>
<p>
<%= text_field_tag :start_date, params[:start_date] %>
<%= text_field_tag :end_date, params[:end_date] %>
<%= submit_tag "Search", :name => nil, class:"btn btn-primary" %>
</p>
<% end %>
</div>
</div>
<table class="table">
<tr>
<th>amount</th>
<th>status</th>
<th>Last Updated</th>
<th>Order Id</th>
<th>manage</th>
</tr>
<% @orders.each do |order| %>
<tr>
<td><%= order.total %></td>
<td><%= order.order_status.name %></td>
<td><%= order.created_at.strftime("%m/%d/%Y") %></td>
<td><%= order.id %></td>
<td><%= link_to 'details', order %> |
<%= link_to 'edit', edit_order_path(order) %> |
<%= link_to 'delete', order_path(order), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
<tr><td colspan="4"></td></tr>
</table>
<div class="row">
<div class="col-sm-12">
<hr>
</div>
</div>
</div>