I''m a Ruby noob, and I''m pretty convinced there''s an elegant way to implement what I''m trying to do. I thought I''d ask if there''s any idioms I could use. Two classes: "team" and "member" joined in a team_members table. In addition to the IDs, the team_members group has a "permissions" column. The permission can take three values: creator, participant or observer. The Member model has three collections: created_teams, , participating_teams, and observed_teams. I use the following construction (this is only the observers collection, but it''s the same for the other two.) has_many :member_teams has_many :observed_teams, :through => :member_teams, :source => :teams, :conditions => "member_teams.permissions = ''#{Observer}'' Works like a charm and I love it. Here''s my question. I''d like to set the permissions field on the member_teams table when I assign it to a collection. For example, t = Team.new member.participating_teams << t That does insert a record in the join table, but I have to access the join record manually and set the permission, otherwise it''s null. I''d like it to be set to "participant" in the instance above, or "observer" if added to the observed_teams collection, etc. Is there a Ruby Way? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
In case anybody hits this in a search, I accomplished this via the "after_save" method of the model. def after_save user_resources.each do |ur| if !ur.user_type if observers.detect {|user| user.id == ur.user_id } ur.user_type = User::Observer ur.save! elsif participants.detect {|user| user.id == ur.user_id } ur.user_type = User::Participant ur.save! elsif creators.detect {|user| user.id == ur.user_id } ur.user_type = User::Creator ur.save! end end end end On Apr 3, 9:24 am, "Rod" <rodf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a Ruby noob, and I''m pretty convinced there''s an elegant way to > implement what I''m trying to do. I thought I''d ask if there''s any > idioms I could use. > > Two classes: "team" and "member" joined in a team_members table. In > addition to the IDs, the team_members group has a "permissions" > column. The permission can take three values: creator, participant or > observer. > > The Member model has three collections: created_teams, , > participating_teams, and observed_teams. I use the following > construction (this is only the observers collection, but it''s the same > for the other two.) > > has_many :member_teams > has_many :observed_teams, > :through => :member_teams, > :source => :teams, > :conditions => "member_teams.permissions = ''#{Observer}'' > > Works like a charm and I love it. Here''s my question. > > I''d like to set the permissions field on the member_teams table when I > assign it to a collection. For example, > > t = Team.new > member.participating_teams << t > > That does insert a record in the join table, but I have to access the > join record manually and set the permission, otherwise it''s null. I''d > like it to be set to "participant" in the instance above, or > "observer" if added to the observed_teams collection, etc. > > Is there a Ruby Way?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Whups, accidental post. Darn touchpads. Anyhow, I accomplished it as below (some class names have changed, I''m sure you can get it though) but it seems tres ugly. If there''s a better way I''m all ears. Thanks, Rod On Apr 3, 11:10 am, "Rod" <rodf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In case anybody hits this in a search, I accomplished this via the > "after_save" method of the model. > > def after_save > user_resources.each do |ur| > if !ur.user_type > if observers.detect {|user| user.id == ur.user_id } > ur.user_type = User::Observer > ur.save! > elsif participants.detect {|user| user.id == ur.user_id } > ur.user_type = User::Participant > ur.save! > elsif creators.detect {|user| user.id == ur.user_id } > ur.user_type = User::Creator > ur.save! > end > end > end > end >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---