# Existing magic to prevent db redundancy?

**URL:** <https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297>\
**Category:** Rails\
**Created:** [May 22, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297 "2006-05-22T22:18:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 22, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/1 "2006-05-22T22:18:29Z")

</div>

suppose users have a favorite colour, linked to with a color\_id field.  
suppose also they have a pet rock, which is one of many possible  
colors…

is there a ‘best practice’ in order to avoid having ‘blue’ appear in the  
Color table 100 times. can validates\_uniqueness\_of be twisted into  
handling this case? should i be adding something to the initialize()  
method of Color to search for itself and return an existing one if the  
name matches? perhaps set the unique attribute right on the color table  
in the DB and handle inserts with rescue that returns an existing  
record? just looking for a pointer in the right direction. obviously any  
solution to this is a few lines at most but i am wondering if i didnt  
just miss some built-in AR magic or if someone has a preferred  
technique…

---

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 22, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/2 "2006-05-22T22:31:14Z")

</div>

On Monday, May 22, 2006, at 10:18 PM, cdr wrote:

> record? just looking for a pointer in the right direction. obviously any  
> solution to this is a few lines at most but i am wondering if i didnt  
> just miss some built-in AR magic or if someone has a preferred  
> technique…
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Try ‘has\_and\_belongs\_to\_many’ (habtm)

rocks  
id  
user\_id  
name

colors  
id  
name

colors\_rocks  
color\_id  
rock\_id

Rock habtm colors  
Rock belongs\_to user  
Color habtm rocks

pet = Rock.new \<\< Color.find\_by\_name(“Blue”)  
pet.save

If you wanted to make sure the rock was only one color, you could clear  
the associations before making a new one.

\_Kevin

---

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 22, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/3 "2006-05-22T22:37:55Z")

</div>

The standard way of handling this is a has\_and\_belongs\_to\_many  
relationship.

This means that many users associate with blue, and blue associates  
with many users.

* * *

| users | -------\< | colors\_users | \>------- | colors |

* * *

```
                | color_id |
                | user_id |
                ----------------

```

Rails supports this via has\_and\_belongs\_to\_many, but it’s  
really a relational database concept that is supported by  
Rails, i.e. the concept existed well before Rails. 🙂

You can create as many join tables against colors as you  
need. In your example, you’d need the one against users  
as shown above (to handle the users’ favorite colors),  
and another against whatever table associates pet rocks  
with that user.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 22, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/4 "2006-05-22T22:41:33Z")

</div>

Greetings,

I’m trying to test some of my controller’s protected methods and I’m  
wondering how other folks handle this.

Should I be testing protected methods?

I’ve tried searching with google on this topic and not much has turned  
up. If anyone knows about good articles  
on this subject, that would also be appreciated.

-Steven

---

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 22, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/5 "2006-05-22T23:19:52Z")

</div>

Tom M. wrote:

thanks for your prompt response (Kevin also) . actually ive got a  
polymorphic join table to associate the colors to where they need to go  
(actually theyre Locations, and i wanted to avoid excessive geolocation  
lookups), but was wondering if there was some kind of find\_or\_create().  
and i see something about it in the AR changelog and as a convenience  
function in Typo… so. next time i must just think ‘what would the  
rails method be called if it existed’, and try it in irb…

ps you got a Figlet thing to generate those diagrams?

---

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 22, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/6 "2006-05-22T23:06:27Z")

</div>

On May 22, 2006, at 03:35 PM, Steven H. wrote:

> I’m trying to test some of my controller’s protected methods and  
> I’m wondering how other folks handle this.
> 
> Should I be testing protected methods?

Obviously, the answer to that is, “you should always test all of your  
applications behavior.” ;-\>

Now, as to how to go about this, for protected/private methods, it’s  
not really that hard. They key thing is you can’t test those methods  
directly, since you can only call them from within the Controller  
class. Hopefully, your protected method is generating or setting an  
instance variable or session value. All you need to do is test the  
value of that data as part of one of your functional tests. If your  
method returns different data, based on the action that calls it,  
then you’ll need to test those additional return values in the  
appropriate functional test methods.

One last thing… if your protected method is performing a standard  
operation on a model class, you might want to look into moving that  
behavior out of the controller and into the model class. That way,  
you can easily Unit Test it. I generally try to keep my functional  
tests to verifying templates, redirects and assert\_tag values. If I  
need to generate specific data that’s used across several controllers/  
actions, hopefully that data is coming from one of my model classes,  
so that I can test it irrespective of where it might be accessed.

-Brian

---

<div class="post-metadata">

**Author:** ![carmen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@carmen](https://www.ruby-forum.com/u/carmen)\
**Post date:** [May 23, 2006, 12:28am UTC](https://www.ruby-forum.com/t/existing-magic-to-prevent-db-redundancy/61297/7 "2006-05-23T00:28:13Z")

</div>

On May 22, 2006, at 2:19 PM, cdr wrote:

> ps you got a Figlet thing to generate those diagrams?

Nope, just years of experience, and enough years behind  
me to be patient enough to knock them out. 🙂

–  
– Tom M.
