Hi I''m working on a project that has users and groups, users can be members of a group *or* they can just be a user - what''s going to be the best way to deal with this? I understand the normal has_many relationship just not sure how to do this one. Will try to post some code snippets tomorrow. Thanks, Cameron -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cc9e3cfb-9180-4527-af5d-b4b3768a5eb6%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Jun 24, 2013 at 8:54 AM, Cameron Gilroy <me-MbFXJOG/KBXb/0FH0DggAwC/G2K4zDHf@public.gmane.org> wrote:> users can be members of a group or they can just be a > user - what''s going to be the best way to deal with this?has_many :through seems perfect for this, given the many-to-many nature and the possibility you''ll want to have additional info about the relationship (like when they joined, positions held within the group, etc.). -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQigPKZPovetba%2B-momg2pM7sSyTaA2AA_75bA9J0jp8pig%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On 24 June 2013 13:54, Cameron Gilroy <me-MbFXJOG/KBXb/0FH0DggAwC/G2K4zDHf@public.gmane.org> wrote:> Hi > > I''m working on a project that has users and groups, users can be members of > a group or they can just be a user - what''s going to be the best way to deal > with this?If the user can only be a member of one group then use User belongs_to group. If a particular member does not belong to a group then just leave group_id nil. You will obviously have to allow for user.group == nil in the code where appropriate. If he can belong to many (or no) groups then use has_many :through as Dave suggested. You can test user.groups to see if he is in any groups. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLt99XE6J1YK%2BGk-rjM_JMAt%2BtbZpWrsZ%2BFd%3D%3DkfWx0srA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Jun 24, 2013 at 10:11 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> If the user can only be a member of one group then use User belongs_to > group.Oh, good point. I was assuming (and you know what happens then!) that a User could belong to many Groups. -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQih-VLfGY1boeJnOsOSRn-Qi1WSrBG8Sd4tW-DUsbx%2BVQA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Thanks Dave & Colin! That was how I had hoped it would work! On Monday, June 24, 2013 10:54:14 PM UTC+10, Cameron Gilroy wrote:> > Hi > > I''m working on a project that has users and groups, users can be members > of a group *or* they can just be a user - what''s going to be the best way > to deal with this? > I understand the normal has_many relationship just not sure how to do this > one. Will try to post some code snippets tomorrow. > > Thanks, Cameron >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0f7b85cd-37e2-417b-8f50-35f3f5ec53b5%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Here is an update, I have it working as only a group or as only a non-group user. I have below the create controller and was wondering what would be the best way to allow associations and non-associated records. Thanks def create if Group.nil? @group = Group.find(params[:group_id]) @user = @group.users.create(params[:user]) else @user = User.new @user.ip_address = request.remote_ip @user.ua_string = request.user_agent @user = User.new(params[:user]) end respond_to do |format| if @user.save && @user.group_id == nil format.html { redirect_to "/", notice: ''Thank you for registering!'' } elsif @user.save && @user.group_id != "" format.html { redirect_to group_path(@group.token), notice: ''Thank you for registering!'' } else format.html { render action: "new" } format.json { render json: @user.errors, status: :unprocessable_entity } end end end On Monday, June 24, 2013 10:54:14 PM UTC+10, Cameron Gilroy wrote:> > Hi > > I''m working on a project that has users and groups, users can be members > of a group *or* they can just be a user - what''s going to be the best way > to deal with this? > I understand the normal has_many relationship just not sure how to do this > one. Will try to post some code snippets tomorrow. > > Thanks, Cameron >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/db8099f1-e254-428d-9462-01174f13bda6%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Jun 25, 2013 at 9:55 PM, Cameron Gilroy <me-MbFXJOG/KBXb/0FH0DggAwC/G2K4zDHf@public.gmane.org> wrote:> respond_to do |format| > if @user.save && @user.group_id == nil > format.html { redirect_to "/", notice: ''Thank you for registering!'' > } > elsif @user.save && @user.group_id != "" > format.html { redirect_to group_path(@group.token), notice: ''Thank > you for registering!'' } > else > format.html { render action: "new" } > format.json { render json: @user.errors, status: > :unprocessable_entity } > endThis will do (or at least try) the save twice, if the user has a group. I''d recommend: if it saves redirect to (if it has a group, the group, else /) else give error msg end Also, look closely at the way you''re checking for a group. The first option will trigger if the group is nil, the second if it''s not "" (empty string)... but what if it *is* ""? That''s neither nil nor not-"", so it will take the "else" path and pretend like it didn''t save. I suggest you use .blank? (or its inverse, .present?) to tell if it''s there or not. Both nil and "" count as blank (and not present). -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQiioC7s_WeL8f%3DOTp0Okfw7A5QsxAe%2BuMP6LkM5Kuc1CiA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On 26 June 2013 02:55, Cameron Gilroy <me-MbFXJOG/KBXb/0FH0DggAwC/G2K4zDHf@public.gmane.org> wrote:> Here is an update, I have it working as only a group or as only a non-group > user. I have below the create controller and was wondering what would be the > best way to allow associations and non-associated records. Thanks > > def create > if Group.nil?Can you explain what the above line is for? Colin> @group = Group.find(params[:group_id]) > @user = @group.users.create(params[:user]) > else > @user = User.new > @user.ip_address = request.remote_ip > @user.ua_string = request.user_agent > @user = User.new(params[:user]) > end > > respond_to do |format| > if @user.save && @user.group_id == nil > format.html { redirect_to "/", notice: ''Thank you for registering!'' > } > elsif @user.save && @user.group_id != "" > format.html { redirect_to group_path(@group.token), notice: ''Thank > you for registering!'' } > else > format.html { render action: "new" } > format.json { render json: @user.errors, status: > :unprocessable_entity } > end > end > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLszNroLRpSmokgNqQdGDU7m%2BZ6PuSh-izqVDqtZ3%3DpUmg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.