0

ユーザーとシフトがあります。ユーザーには多くのシフトがあります。

class User < ActiveRecord::Base
  has_many :shifts
end

class Shift < ActiveRecord::Base
  belongs_to :user
end

私のユーザーの1人は、自分のシフトの1つを実行できないため、別のユーザーと交換したいと考えています。

これを安らかに行うための最良の方法は何ですか?2つのシフトを同時に更新する必要があるようです。交換する必要があるシフトと、交換する必要があるシフトです。したがって、これは、単一のモデルを変更することを目的としたeditおよびアクションには実際には適合しません。update

4

2 に答える 2

1

This doesn't fit neatly with any of the default RESTful routes for a single resource (member route). One possibility is to add a new RESTful collection route:

# routes.rb
resources :users do
  collection do
    post 'swap'
  end
end

More at: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

于 2012-10-04T18:51:45.867 に答える
1
def change_shift(shift_id_or_object, alt_user)
  shift = Shift.find(shift_id_or_object) unless shift_id_or_object.respond_to? :user
  shift.user = alt_user
  shift
end
于 2012-10-04T18:41:33.150 に答える