Multiple entries in: has_many through association

I need help with the development of the rails that I am working on using rails 3. This application was provided to me a few months ago immediately after its creation, and since then I have loved Ruby very much.

I have a set of projects that can have resources assigned through a command table.

The team record has a start date and an end date (that is, when the resource was assigned and removed from the project).

If a user has been assigned and disconnected from the project, and at a later date they will be assigned back to the project, instead of writing the end date, I want to create a new entry in the Commands table so that I can track the dates when the resource was assigned specific project.

So my question is: is it possible to have multiple entries in: has_many through an association?

Here are my associations:

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 

I also have the following function in 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 
+4
source share
1 answer

Of course, you can have several associations. has_many accepts the: uniq parameter, which you can set to false, and as the documentation notes, it is especially useful for: via rel'ns.

Your code will find the existing team and install deassociated though, instead of adding a new team (which will be better called TeamMembership, I think)

I think you just want to do something like this:

  • add member for active memberships (but use uniq: => true in this:

     has_many :teams has_many :resources, :through => :teams, :uniq => false has_many :active_resources, :through => :teams, :class_name => 'Resource', :conditions => {:deassociated => nil}, :uniq => true 
  • when adding, adding to active_resources, if it does not exist, and "disconnect" from remote commands:

     member_ids.each do |id| resource = Resource.find(id) #you'll probably want to optimize with an include or pre-fetch active_resources << resource # let :uniq => true handle uniquing for us end teams.each do |team| team.deassociate! unless member_ids.include?(team.resource.id) # encapsulate whatever the deassociate logic is into a method end 

much less code, and much more is idiomatic. In addition, the code now more clearly reflects business modeling.

caveat: I did not write a test application for this, the code may be missing a part or two

0
source

All Articles