检查用户是否拥有传入和传出的服务预订请求

时间:2013-12-24 08:48:53

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

用例:用户创建服务并且可以拥有许多服务。用户可以通过服务预订来预订另一个用户的服务。用户可以接受/拒绝其他用户的服务预订。

我正在尝试通过检查服务预订模型来显示当前用户为其他用户服务所做的传出服务预订,并检查/显示其他用户在myservicebookings页面上为当前用户服务做的传入预订。

Myservicebookings视图如下:

<h1>My Service bookings</h1>
<% if @owns_s %>
<table>
  <tr>
    <th><%= sortable "date" %></th>
    <th><%= sortable "time" %></th>
    <th><%= sortable "service name" %></th>
  </tr>

  <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 %>
</table>

<%else%>
<%= "You have no incoming service booking requests"%>
<%end%>

<% if @owns_sb %>
<table>
  <tr>
    <th><%= sortable "date" %></th>
    <th><%= sortable "time" %></th>
    <th><%= sortable "service name" %></th>
  </tr>

  <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 made no outgoing service booking requests"%>
<%end%>
<%= will_paginate @servicebookings %>
<%= link_to "Homepage", :controller => "welcome", :action => "index" %>

在我的servicebookings控制器中,我有以下内容来检查用户是否拥有服务或服务预订,目前它只返回所有服务和服务预订,而不是仅显示当前用户创建的另一个用户预订的服务(传入请求)而不是显示当前用户预订的服务(传出请求)。谁能在这里给出一些提示?非常感谢你们。非常感谢。

def myservicebookings
    @servicebookings = current_user.servicebookings.includes(:user).search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 4, :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

    @owns_sb = owns_servicebooking
    @owns_s = owns_service

  end

服务预订模式:

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

服务模式:

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

1 个答案:

答案 0 :(得分:1)

has_many:通过

我认为你会受益于has_many :through association

您的ServiceBooking模型似乎是一个加入模型,需要引用booking_idservice_id

您目前设置系统的方式是直接从此模型中提取。我认为你最好将它与其他型号配合使用。您实际上只需要稍微调整代码以影响此更改:

#app/models/Service.rb
Class Service < ActiveRecord::Base
    belongs_to :user

    has_many :service_bookings
    has_many :bookings, :through => :service_bookings
end

#app/models/Booking.rb
Class Booking < ActiveRecord::Base
    belongs_to :user

    has_many :service_bookings
    has_many :services, :through => :service_bookings
end

#app/models/ServiceBooking.rb
Class Service < ActiveRecord::Base
    belongs_to :service
    belongs_to :booking
end

这样您就可以从相对模型中提取实际数据,而不仅仅依赖于ServiceBooking模型

之所以如此,因为您可以添加额外的属性来加入Rails中的模型,您将能够包含其他字段,例如user_idinboundoutbound


您的代码

我会这样做:

#config/routes.rb
resources :users do 
    resources :bookings
    resources :services
end

#app/controllers/users_controller.rb
def index
    user = User.find(params[:id])
    @bookings = user.bookings
    @services = user.services
end 

#app/controllers/bookings_controller.rb
def index
    user = User.find(params[:user_id])
    @bookings = user.bookings
end

#app/controllers/services_controller.rb
def index
    user = User.find(params[:user_id])
    @services = user.services
end

这将允许您显示每个用户的实际预订/服务(用户index页面上的所有服务/预订; bookingsservices索引操作的所有相对记录)


<强>验证

如果您遵循这些想法,验证会变得更有条理

您可以在before_create模型上使用ServiceBooking函数(以检查对象是否与user_id匹配),或者您可以执行一些基于控制器的验证以查看{{1}是一致的

我还应该提一下,检查user_idbooking的所有权是否会转移到其他模型(I.E service service