Reputation: 3
Okay, I am attempting to perform the following which I know how to do so in PHP but not in Ruby on Rails:
SELECT * FROM calls WHERE (status = 'open') OR (status = 'pending')
Simply look in the database in the calls table for open or pending status.
I'd like to do this in Rails. Currently I've got this in my controller:
class CallsController < ApplicationController
def index
@calls = Call.all
end
end
And this is my view:
<table>
<% @cals.each do |call| %>
<tr>
<th><%= link_to call.id, call_path(call) %></th>
<th><%= call.cname %></th>
<th><%= call.cbn %></th>
<th><%= call.email %></th>
<th><%= call.operator %></th>
<th><%= call.reason %></th>
<th><%= call.ticket %></th>
<th><%= call.created_at %></th>
<th><%= call.tier %></th>
<th><%= call.status %></th>
<th><%= link_to 'Edit', edit_call_path(call) %></th>
<th><%= link_to 'Remove', call_path(call), method: :delete, data: { confirm: 'Are you sure?' } %></th>
</tr>
<% end %>
</table>
Upvotes: 0
Views: 52
Reputation: 18672
Try this:
def index
@calls = Call.where(status: ['open', 'pending'])
end
It's considered good practice to make sure these where
calls don't leak into your controller, because they're exposing a lot of the internals of Call
. You can define a scope inside the Call
class:
class Call < ActiveRecord::Base
scope :open_or_pending, -> { where(status: ['open', 'pending']) }
end
And then use that inside your controller:
def index
@calls = Call.open_or_pending
end
Note that now, the controller doesn't need to know that Call
has a status
field, and what its values can be.
Upvotes: 2