Rails 3 を使用して、現在取り組んでいる Rails 開発について助けが必要です。このアプリは、数か月前に開始直後に提供され、それ以来、Ruby が好きになりました。
チーム テーブルを介してリソースを割り当てることができる一連のプロジェクトがあります。
チーム レコードには、開始日と終了日があります (つまり、リソースがプロジェクトに割り当てられ、割り当てが解除された日)。
ユーザーがプロジェクトに割り当てられ、プロジェクトから割り当て解除され、後日プロジェクトに再度割り当てられる場合、終了日を上書きするのではなく、Teams テーブルに新しいエントリを作成して、次のことができるようにしたいと考えています。リソースが特定のプロジェクトに割り当てられた日付を追跡します。
だから私の質問は、関連付けを介して :has_many に複数のエントリを持つことは可能ですか?
ここに私の協会があります:
class Resource < ActiveRecord::Base
has_many :teams
has_many :projects, :through => :teams
end
class Project < ActiveRecord::Base
has_many :teams
has_many :resources, :through => :teams
end
class Team < ActiveRecord::Base
belongs_to :project
belongs_to :resource
end
Project.rb には次の関数もあります。
after_save :update_team_and_job
private
def update_team_and_job
# self.member_ids is the selected resource ids for a project
if self.member_ids.blank?
self.teams.each do |team|
unless team.deassociated
team.deassociated = Week.current.id + 1
team.save
end
end
else
self.teams.each do |team|
#assigning/re-assigning a resource
if self.member_ids.include?(team.resource_id.to_s)
if team.deassociated != nil
team.deassociated = nil
team.save
end
else
#de-assigning a resource
if team.deassociated == nil
team.deassociated = Week.current.id + 1
team.save
end
end
end
y = self.member_ids - self.resource_ids
self.resource_ids = self.resource_ids.concat(y)
self.member_ids = nil
end
end
end