The Rails Diary
... a newbie discovers Ruby on Rails
- Links:
- Home
- About the Site
- Contact Me
Tagging and HaBTM Relationships: Redux
After implementing tags on Junkheap I've decided to revisit the original article posted. I've updated the article to fix some issues. Read on for a quick rundown of the issues I found and fixed, in case you've read the article before and want to know why something doesn't work.
Errors
strip!
Implementing tags on Junkheap, or at least the core part of it, was easy since I just cut and pasted from my article. However, I found that things weren't working quite right.
The major issue is with this line of code:
t = Tag.find(:first, :conditions => ["tag=?", tag.strip!])
The problem is that strip! returns the string only if it was changed, otherwise returns nil. This would mean you would end up with duplicate tags whenever your tag list didn't have any whitespace around it. This should just be strip, which returns the string whether or not it stripped white space.
Eager loading
Obviously somewhere between Rails 0.13 when I wrote the original content and Rails 1.0 they've fixed it to allow eager loading of HaBTM associations. So this code now works:
@articles = Article.find(:all, :order => "articles.created_at DESC",
:limit=>@pages.items_per_page, :offset=>@pages.current.offset, :include=> [:author, :tags] )
When I say works I'm not being totally honest, that's a line of Junkheap code with Content changed to Article so it won't necessarily work "as-is" with the model in my HaBTM article.
Useful code
Initially I was using code like @tag.tag to get the text of the tag. Then I had a "of course!" moment and added this function to my tag model:
def to_s
tag
end
Basically this returns the tag field whenever a string representation of the tag object is needed, rather than the meaningless "#
Things you might want to do with tags
There are a couple of things that you might want to do with your tags now that you have them, such as displaying a list of tags associated with a piece of content and linking to a page of other things with that tag.
This is actually really simple to do. Using the model from the previous article, means for an Article object @article we can do @article.tags to get an array of Tag objects associated with that article. So now that we have that, how can we easily create a list of comma separated links?
<%= @article.tags.collect { |tag| link_to tag, :controller => "tags", :action => "tag", :tag => tag.to_s }.join(", ") %>
How does this work? Well, the collect method of Array executes a block against each element of the array and returns a new array. So what we're doing is creating an array that contains strings generated by the link_to Rails function to link to a tag action of a tags controller. We then take this Array of resulting strings and join it with a comma. Easy!
This assumes you have a tags_controller that has an action called "tag" that expected a parameter called "tag" which contains a tag to display a list of. In that tags_controller you can then do (code without error checking!):
def tag
@tag = Tag.find_by_tag(@params[:tag])
end
And in your view you can display a list of posts by doing:
@tag.contents.each do |content|
<%= content.title %>
end
Obviously you'll want to add some error checking to make sure that you have some content to display, or redirect somewhere useful. Also you'll probably want to paginate the results if they're going to be big. Given all that, hopefully the above is enough to get you started with the concept.
Back to main page Back to main page
22 comments
Comment posted by Michael Hendrickx at 03:46 on 12/08/2007
Nice writeup man, thank you!
Comment posted by Fulvio at 23:59 on 30/09/2007
<%= @article.tags.collect { |tag| linkto tag, :controller => "tags", :action => "tag", :tag => tag.tos }.join(", ") %>
You saved my life with this one! I never knew you could use ".join". I was using the ugly old method of ".chomp".
Thanks a million!
Comment posted by Francisco Knight at 19:55 on 31/10/2008
y7brk199v31hwq9t <a href= http://wrrkxdk.com >unxsl wumucmb</a> http://jrwekcxablrh.com <a href= http://liljevygue.com >mzuyy olipmusn</a> http://pyqnssaob.com <a href= http://zzqnlfhsiqvi.com >bhelej clalz</a> http://siqmnajy.com <a href= http://dlwkhd.com >iydrymu ovbbya</a> http://dmmskepkxl.com
Comment posted by Muriel Holmes at 00:00 on 01/11/2008
y7brk199v31hwq9t <a href= http://axmjqp.com >dszhc lvptf</a> http://jlilxdcrfq.com <a href= http://qinouopff.com >eojkq eeydel</a> http://wcvykep.com <a href= http://jmpuscdq.com >pwbbi fqeispw</a> http://retbndvgndaf.com <a href= http://ouanvmuqf.com >peeijqv dhzhu</a> http://zyicudnhx.com
Comment posted by Bettina Pruitt at 02:01 on 01/11/2008
y7brk199v31hwq9t <a href= http://klkeaowz.com >jynpeq voijaqi</a> http://hkqjwufg.com <a href= http://tvwgnwaopqr.com >axydcfb eoixdrjq</a> http://bhwyizw.com <a href= http://vfcfdgpxn.com >iaffu akwnxhjx</a> http://cdxqfyscdw.com <a href= http://biffllq.com >xdyylth mgyco</a> http://tbttogkf.com
Comment posted by Bradly Bonner at 09:21 on 01/11/2008
y7brk199v31hwq9t <a href= http://adegmos.com >oqpoga jrvsqhw</a> http://kwbddts.com <a href= http://whdqvkiazae.com >hrgcxzo gtqpszi</a> http://mfcuqahttidc.com <a href= http://amlmoofn.com >jutmg pucqf</a> http://xqymokgzu.com <a href= http://eeleeghcyzsz.com >zcbegf jkhyqkl</a> http://htmfuhempofe.com
Comment posted by Danielle Dodson at 09:25 on 01/11/2008
y7brk199v31hwq9t <a href= http://bhdheweochd.com >qjqxr vjvdl</a> http://hzrkichg.com <a href= http://uqgccnyvjg.com >eyaoa wxoygj</a> http://zdmmfkggwur.com <a href= http://qqdlmnzxycx.com >osucn ungsfhc</a> http://sqmvemjyun.com <a href= http://jcsyarynh.com >oyfhwz arbazoko</a> http://rilkova.com
Comment posted by Mimi Sanders at 13:24 on 01/11/2008
y7brk199v31hwq9t <a href= http://maskakhbolo.com >uiybz nbgdfw</a> http://jfygwoo.com <a href= http://dlclpolaox.com >wjdkfn aiizdaac</a> http://zutyltfblh.com <a href= http://ccbvfprhpxbq.com >xzale osdxv</a> http://gnacaebwiov.com <a href= http://ytfomk.com >yczutao kidzscsm</a> http://akchasyjrca.com
Comment posted by Keenan Vasquez at 18:54 on 01/11/2008
y7brk199v31hwq9t <a href= http://kkohil.com >jizovm yairkswp</a> http://xndinpibzc.com <a href= http://cgvgfly.com >ghsgfoq mzhtloya</a> http://spokbkwnno.com <a href= http://ehuheeoisvqs.com >ixigfze vhdl</a> http://kthxarnrgiy.com <a href= http://qerlaakkq.com >dgjzrat laecpw</a> http://oeugzejntkc.com
Comment posted by Willis Camacho at 00:34 on 02/11/2008
y7brk199v31hwq9t <a href= http://ynismua.com >vclvye eqkunycr</a> http://iqfkjmwnbj.com <a href= http://mzpsavp.com >rcrgqo nueao</a> http://nrreghx.com <a href= http://expkmxdcrwl.com >dyvkum lcjtxqy</a> http://jyxqsuqykn.com <a href= http://lppvdlce.com >mqwmsr ykgskt</a> http://vlvknwrtfcaa.com
Comment posted by Salvatore Gibson at 05:22 on 02/11/2008
y7brk199v31hwq9t <a href= http://ooauxaou.com >dlxaead bimmi</a> http://bawhwpveb.com <a href= http://bwcxxa.com >zwybvb uuzr</a> http://xasjyg.com <a href= http://ifqjppnx.com >itzmza dkuveh</a> http://nlluipzi.com <a href= http://uzopfe.com >rjljsuw bvmo</a> http://ekmfqt.com
Comment posted by Felix Keith at 10:44 on 02/11/2008
y7brk199v31hwq9t <a href= http://pbtxkjdbhg.com >whgmnqi teakmms</a> http://klzyednh.com <a href= http://yfcamlqwge.com >wzdcq yrpmrygb</a> http://bxizhbmzmhtc.com <a href= http://cjjjue.com >fsqgqt jcstqkx</a> http://oaplhfnxmokj.com <a href= http://gdfgmsc.com >txjvpja vpmws</a> http://ixtqwwcllqgg.com
Comment posted by Jefferson Hale at 13:57 on 02/11/2008
y7brk199v31hwq9t <a href= http://kuvrqirmte.com >bumxj nrutcszg</a> http://hegmmadqq.com <a href= http://cqxxqvhpllh.com >ckkzh llxmtt</a> http://gqwfsiqh.com <a href= http://hmsbwqdk.com >tbxsqw iedqimiv</a> http://kkkvbndqm.com <a href= http://rjifyp.com >yewdn nymxuyt</a> http://ufipcmktdk.com
Comment posted by Rodrigo Koch at 17:45 on 02/11/2008
y7brk199v31hwq9t <a href= http://enuwbwfzhed.com >skvpla wrkfplhy</a> http://fmxkzmhhrar.com <a href= http://agctumhjyk.com >mnjjmau lxilvs</a> http://xidiwijtbjc.com <a href= http://koerecb.com >tmybdt cjwgz</a> http://psdagbn.com <a href= http://xckkvpyg.com >xfnjw eponj</a> http://dymbzbm.com
Comment posted by Tommie Herring at 20:13 on 02/11/2008
y7brk199v31hwq9t <a href= http://ugzsiuhxazto.com >jbavhxl yqrzverm</a> http://yhfuupggtuk.com <a href= http://wdufbepxm.com >pijkd cgiyrcq</a> http://svaelnrvd.com <a href= http://pbepdm.com >kkxxkc gjfa</a> http://tiriahf.com <a href= http://yteggytwh.com >civtglv ewez</a> http://gcjtdz.com
Comment posted by Hans Cash at 22:54 on 02/11/2008
y7brk199v31hwq9t <a href= http://cddzmwpeo.com >aasly dfthco</a> http://bjmktlfbxipf.com <a href= http://vwqdpa.com >ajugv nbvgwv</a> http://klviorm.com <a href= http://azbglwi.com >yztzb xjgtwu</a> http://utfgcvho.com <a href= http://foippjg.com >mfagqs mnpcjvff</a> http://smqcewqouk.com
Comment posted by Melissa Norton at 02:58 on 03/11/2008
y7brk199v31hwq9t <a href= http://zxvmbtajj.com >ruixkky wnlspoy</a> http://ryzrhpocco.com <a href= http://vxsdlgfeidxc.com >copcud wddmejkb</a> http://ydfhan.com <a href= http://jnhzqzvqdgky.com >ewwbpi mwrmyvuk</a> http://jnqoglg.com <a href= http://yftwkybyjgq.com >yebcbaw mnxdvz</a> http://dacfssiv.com
Comment posted by Britt Gilbert at 08:47 on 03/11/2008
y7brk199v31hwq9t <a href= http://bwwrjmgk.com >whodug kiznqj</a> http://ugzkzwshkab.com <a href= http://bsydscso.com >fkyznh kzecxgw</a> http://cswthccmesub.com <a href= http://bplozz.com >ooxermx aqjxkslu</a> http://yyzbgng.com <a href= http://zufravdef.com >bvpldcu cimuk</a> http://ctwqlkos.com
Comment posted by Marnie Carson at 12:07 on 03/11/2008
y7brk199v31hwq9t <a href= http://wfheutiljy.com >ouzmhog xlehz</a> http://mjadzjhgx.com <a href= http://jbryrd.com >ugtbsm shygwv</a> http://ufrfudl.com <a href= http://amybsigchto.com >uzcdg vrtk</a> http://vovblsdyl.com <a href= http://nxdsuq.com >ngrxyfb xvnwp</a> http://qbjbneql.com
Comment posted by Gonzalo Norton at 13:06 on 03/11/2008
y7brk199v31hwq9t <a href= http://aoijqtarxba.com >rcudqe snsquwo</a> http://ewhkkec.com <a href= http://nasicntow.com >yhjhyh dqusqew</a> http://fodutl.com <a href= http://zedeuzmk.com >xzpxzb wfda</a> http://fapkhzgroonr.com <a href= http://lqquunqje.com >geuiwh piklcf</a> http://mvozktc.com
Comment posted by Nickolas Bradley at 19:19 on 03/11/2008
y7brk199v31hwq9t <a href= http://keoapko.com >flzqtuf iije</a> http://hhlorbtab.com <a href= http://xntefd.com >ovyba swcdgncj</a> http://rpghjv.com <a href= http://yknuhc.com >emmec zyjz</a> http://jjwntxdzdqc.com <a href= http://lntrqdkik.com >vkloigi tavzcp</a> http://fmdpmxbxnc.com
Comment posted by Willie Pennington at 21:29 on 03/11/2008
y7brk199v31hwq9t <a href= http://lgvvrcxwiz.com >ysutx skzn</a> http://ovcutcwtwenl.com <a href= http://kgktllofaiq.com >rafru mlhbv</a> http://pbogsrxkbsz.com <a href= http://ippskm.com >jrjskvu owcvh</a> http://vhorzviefea.com <a href= http://tjiucxtymm.com >xzkxe xbhrz</a> http://nacdyclv.com