Reputation: 371
I have service and servicebooking models. I want to be able to check whether the current user owns a service and display the servicebookings that have been made for that users services.
I am trying to display this in a myservicebookings page.
This is the myservicebookings action in the servicebookings_controller, in the commented line Iam trying to check whether the current user is the owner of any of the servicebookings made or the owner of any of the services booked, I need to advise on how to go about this?
def myservicebookings
@servicebookings = current_user.services.servicebooking.find(params[:user_id])
@servicebookings = current_user.servicebookings.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
This is the myservicebookings view, Iam trying to display both incoming/outgoing requests for services for the current user:
<h1>My Service bookings</h1>
<table>
<tr>
<th><%= sortable "date" %></th>
<th><%= sortable "time" %></th>
<th><%= sortable "service name" %></th>
</tr>
<% if @servicebookings.any? %>
<h4>Incoming requests:</h4>
<% @servicebookings.each do |servicebooking| %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
<td><%= link_to "View this booking", servicebooking_path(servicebooking) %></td>
</tr>
<% end %>
<h4>Outgoing requests:</h4>
<% @servicebookings.each do |servicebooking| %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
<td><%= link_to "View this booking", servicebooking_path(servicebooking) %></td>
</tr>
<% end %>
</table>
<%else%>
<%= "You have no incoming or outgoing service booking requests"%>
<%end%>
<%= will_paginate @servicebookings %>
<%= link_to "Homepage", :controller => "welcome", :action => "index" %>
Servicebooking model:
class Servicebooking < ActiveRecord::Base
attr_accessible :service_id, :date, :time, :user_id, :service_name, :accept_booking
belongs_to :user
belongs_to :service
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
Services model:
class Service < ActiveRecord::Base
attr_accessible :avatar, :avatar2, :avatar3, :avatar4, :name, :date_available, :time_available, :description, :price, :size, :company_name, :company_details
has_attached_file :avatar, :default_url => "/images/:style/missing.png"
has_attached_file :avatar2, :default_url => "/images/:style/missing.png"
has_attached_file :avatar3, :default_url => "/images/:style/missing.png"
has_attached_file :avatar4, :default_url => "/images/:style/missing.png"
belongs_to :user
belongs_to :event
has_many :comments, dependent: :destroy
has_many :servicebookings
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
Thanks guys, any help is appreciated.
Upvotes: 0
Views: 83
Reputation: 4808
You can do something like this in your controller. Assuming that there is a owner
method on ServiceBooking
and Service
def myservicebookings
@servicebookings = current_user.servicebookings.includes(:user).search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
owns_servicebooking = current_user.servicebookings.detect do |sb|
sb.user == current_user
end
owns_service = current_user.services.detect do |s|
s.user == current_user
end
@is_an_owner = owns_servicebooking and owns_service
end
detect
returns an object if it matches the block and since it is ruby you can use it in a boolean expression to get what you want.
UPDATE: added controller method to the code example above. To use it in your view something like:
<% if @is_an_owner %>
<markup/>
<% end %>
I am pretty certain that this is not the best option in your case but I don't know enough about your stuff to suggest otherwise. It may be a start though. You may want to consider putting parts of this down to the model or into a helper method instead.
Upvotes: 1