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…
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…
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.
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.
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?
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.