Rails公司与员工之间的关系

时间:2018-03-26 20:38:30

标签: ruby-on-rails ruby

我还是Rails的新手并且正在学习构建一个简单的应用程序。 这个想法是有一个用户可以创建一个帖子" list"并且用户可以加入该列表。用户可以加入,或者列表的所有者可以从另一方添加用户及其需要批准。

ERD: https://www.dropbox.com/s/ux0e78g8l3c38p6/erd.pdf?dl=0

如何检查用户是否已从列表视图和用户视图加入列表? 如果用户请求加入列表然后将列表设置为待处理,但我怎样才能允许列表所有者批准呢? 如果列表的所有者请求添加用户,我如何才允许该用户批准该请求? 我应该为ex:requested_by创建新列以检查创建关系的人并检查它吗?或任何其他方式? 感谢

用户模型:user.rb

class User < ApplicationRecord

has_many :employeeships

has_many :pending_listings,   -> {where employeeships: { status: 0 }}, :through => :employeeships, source: :listing
has_many :accepted_listings,  -> {where employeeships: { status: 1 }}, :through => :employeeships, source: :listing

# User owner listing relation
has_many :listings, ->{ order "created_at Desc" }

end

列表模型:listing.rb

class Listing < ApplicationRecord

  has_many :employeeships

  has_many :pending_users,   -> {where employeeships: { status: 0 }}, :through => :employeeships, source: :user
  has_many :accepted_users,  -> {where employeeships: { status: 1 }}, :through => :employeeships, source: :user


  # User owner listing relation
  belongs_to :user
end

员工模式:employeeship.rb

class Employeeship < ApplicationRecord
  belongs_to :user
  belongs_to :listing

  # User status for listings
  enum status: [ :pending, :accepted ]

end

Employeeships controller:employeeships_controller.rb

class EmployeeshipsController < ApplicationController

  def create
    listing = Listing.find(params[:listing_id])
    user = User.find(params[:user_id])
    @user_listing = Employeeship.create(user: user, listing: listing, status: 0)
    flash[:info] = "#{@user_listing.user.name} was successfully added to #{@user_listing.listing.title}"
    redirect_to listing
  end



  def update
    user = params[:user_id]
    listing = params[:listing_id]
    @employeeship = Employeeship.where(user: user, listing: listing).first
    @employeeship.status = 1
    if @employeeship.update(employeeship_params)
      flash[:success] = "Employeeship was successfully accepted."
      redirect_to request.referrer
    end
  end

  def destroy
    user = params[:user_id]
    listing = params[:listing_id]
    @employeeship = Employeeship.where(user: user, listing: listing).first
    @employeeship.destroy
    flash[:success] = "Employeeship was successfully deleted."
    #redirect_to listing_path()
    redirect_to request.referrer
  end


  private
    def employeeship_params
      params.permit(:user, :listing, :status)
    end

end

Routes.eb

resources :users

resources :listings

resources :employeeships, only: [:create, :destroy, :update]

人数/ show.html.rb

<%= link_to 'Join Listing', employeeships_path(user_id: current_user, listing_id: @listing.id), method: :post %>

<% @listing.pending_users.each do |user| %>
    <%= link_to user.name, user_path(user) %>
    <%= link_to 'Accept', employeeship_path(user_id: user, listing_id: @listing.id), method: :put, data: { confirm: "Are you sure?"} %>
    <%= link_to 'Cancel', employeeship_path(user_id: user, listing_id: @listing.id), method: :delete, data: { confirm: "Are you sure?"} %>
<% end %>

0 个答案:

没有答案