# Has\_and\_belongs\_to\_many -- has anyone made this easier yet?

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-has-anyone-made-this-easier-yet/88759>\
**Category:** Rails\
**Created:** [March 14, 2007, 4:04am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-has-anyone-made-this-easier-yet/88759 "2007-03-14T04:04:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [March 14, 2007, 4:04am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-has-anyone-made-this-easier-yet/88759/1 "2007-03-14T04:04:25Z")

</div>

Not that this isnt REALLY EASY already… it is!!! I was just wondering  
if  
someone had already waved their magic wand on this problem. 🙂

I’ve got users and groups… both has\_and\_belongs\_to\_many eachother. I  
want  
to be able to check for the users and groups by ID# or string to make  
the  
coding easier. So in User, I have this:

has\_and\_belongs\_to\_many :groups, :join\_table =\> “users\_groups” do  
def include?(group)  
if group.class == Fixnum  
group = Group.find group  
elsif group.class == String  
group = Group.find\_by\_name group  
end  
super  
end  
end

and in Group, I have this:

has\_and\_belongs\_to\_many :users, :join\_table =\> “users\_groups” do  
def include?(user)  
if user.class == Fixnum  
user = User.find user  
elsif user.class == String  
user = User.find\_by\_name user  
end  
super  
end  
end

… is there a way to do it better, more clearly, with less lines of  
code?

Thanks,  
Tyler

---

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [March 14, 2007, 4:31am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-has-anyone-made-this-easier-yet/88759/2 "2007-03-14T04:31:27Z")

</div>

Tyler MacDonald wrote:

> Not that this isnt REALLY EASY already… it is!!! I was just wondering if  
> someone had already waved their magic wand on this problem. 🙂
> 
> I’ve got users and groups… both has\_and\_belongs\_to\_many eachother. I want  
> to be able to check for the users and groups by ID# or string to make the  
> coding easier.

Could you show some unit tests that reveal what this easier coding is  
supposed to look like?

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![Tyler\_MacDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Tyler\_MacDonald](https://www.ruby-forum.com/u/Tyler_MacDonald)\
**Post date:** [March 14, 2007, 4:42am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-has-anyone-made-this-easier-yet/88759/3 "2007-03-14T04:42:29Z")

</div>

Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Not that this isnt REALLY EASY already… it is!!! I was just wondering if  
> > someone had already waved their magic wand on this problem. 🙂
> > 
> > I’ve got users and groups… both has\_and\_belongs\_to\_many eachother. I want  
> > to be able to check for the users and groups by ID# or string to make the  
> > coding easier.  
> > Could you show some unit tests that reveal what this easier coding is  
> > supposed to look like?

Not a chance. What I was hoping to do was eliminate that “do” block and  
have  
some magic wand to replace it that recognizes that when I do something  
like:

user.groups \<\< “Admin”

or

user.groups \<\< 1

it will just do the right thing.

However, my pride and respect for ruby is brimming right now, so I will  
share my modified acts\_as\_authenticated, which allows you to do:

require\_group :Admin

(or any other group name) in a controller, and have login decisions  
based  
upon that. 🙂

- Tyler
