Forum: Ruby on Rails Relational DB - Related and not Related

41b4d3cf445e51f1b0eb3d5287ef9169?d=identicon&s=25 Cameron Gilroy (Guest)
on 2013-06-24 14:56
(Received via mailing list)
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
Ec2c1a36301b0223c3e281bd2ec3f37a?d=identicon&s=25 Dave Aronson (Guest)
on 2013-06-24 15:19
(Received via mailing list)
On Mon, Jun 24, 2013 at 8:54 AM, Cameron Gilroy <me@camerongilroy.com>
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/.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-06-24 16:12
(Received via mailing list)
On 24 June 2013 13:54, Cameron Gilroy <me@camerongilroy.com> 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.
Ec2c1a36301b0223c3e281bd2ec3f37a?d=identicon&s=25 Dave Aronson (Guest)
on 2013-06-24 16:20
(Received via mailing list)
On Mon, Jun 24, 2013 at 10:11 AM, Colin Law <clanlaw@googlemail.com>
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/.
41b4d3cf445e51f1b0eb3d5287ef9169?d=identicon&s=25 Cameron Gilroy (Guest)
on 2013-06-25 06:31
(Received via mailing list)
Thanks Dave & Colin! That was how I had hoped it would work!
41b4d3cf445e51f1b0eb3d5287ef9169?d=identicon&s=25 Cameron Gilroy (Guest)
on 2013-06-26 03:57
(Received via mailing list)
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
Ec2c1a36301b0223c3e281bd2ec3f37a?d=identicon&s=25 Dave Aronson (Guest)
on 2013-06-26 19:11
(Received via mailing list)
On Tue, Jun 25, 2013 at 9:55 PM, Cameron Gilroy <me@camerongilroy.com>
wrote:

> :unprocessable_entity }
>       end

This 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/.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-06-26 21:46
(Received via mailing list)
On 26 June 2013 02:55, Cameron Gilroy <me@camerongilroy.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.