Thursday, November 16, 2006

The Rails Path: Tracks (Part 1)

The amazing Jamis Buck has teamed up with Michael Koziarski to offer a new coding series called The Rails Way. Some of the comments in their first installment mention the fact that some people new to rails may not have a very strong grounding in Ruby. In order to help those people understand the Rails Way wisdom, I will be explaining parts of their post in the context of a basic beginner to Ruby/Rails.

Acts_as_name_part_finder
(original code here)
The acts_acts_as_name_part_finder macro adds a method called find_by_namepart that allows for one to search by an entire name or a piece of a name. It would be used like this:

You would then be able to do things like:

The point the Rails Way is making is that it is unnecessary to declare an entirely new acts_as_* macro just for the sake of adding a single function. An additional point is that this function is only ever used in one model. The lesson here is don't abstract code into a new macro/file if its only being used in one place. Wait until you see yourself copying and pasting the code into other places before you start abstracting it.


Modules
If you're learning Ruby/Rails, you really should know what modules are and how to use them. For those that need a quick refresher, a module is just a container around some pieces of code. Usually you define methods in a module and then use the "include" keyword to add those methods/code into your class. For example:

If find_by_namepart had been used in multiple places, it would have made sense to define the function inside a module and then use "include" in the places where you need that functionality.


Acts_as_todo_container
(original code here)
The first point the Rails Way makes is that a lot of the original code for the acts_as_todo_container was solely for the purpose of having the database automatically include associated 'has_many' models in a single db query. This allows for:

With this code, if I made a call to Context.find_not_done_todos I would get back a list of Todo's as well as their associated Projects. A great majority of the actual acts_as_todo_container deals with adding in this :find_todos_include functionality. By using the :include option in the has_many association, all of this code can be eliminated and the resulting code simplified to the point where a separate acts_as_todo_container is not necessary. Instead, the Todo handling code can be placed into a basic module and be "included" as normal.

Other Simplifications
Besides removing :find_todos_include, the Rails Way introduced a number of syntactical simplifications:

1. Replacing code like:

The ||= operator checks to see if @not_done_todos is nil. If it is, it sets it to find_not_done_todos. Otherwise it just evaluates (and returns) @not_done_todos. This is exactly what the previous code did in a more succinct form.

2. Replacing code like:

Instead of making a call to Todo.find and trying to manually simulate the has_many relationship through use of statements like "#{self.class.base_class.name.singularize.downcase}_id", they use the has_many association defined in the model to make the simple "self.todos.find" call. This is again a result of moving the :include parameter to the has_many association and allowing the built in rails macros to handle all the complex stuff.

That's everything for this post. If anyone has any questions (no matter how basic) please feel free to ask. To make things clear, I am not trying to steal the Rails Ways' thunder. My goal is to elaborate on their points so that people who don't have much of a Ruby/Rails background can still understand what's going on.

58 comments:

Jonathan Wallace said...

Thanks Shalev. As someone who has yet to finish the Programming Ruby book but has immersed himself in Rails, I appreciate your breakdown. Please keep it up, and I promise to keep picking away at the Pickaxe book.

Anonymous said...

关爱生命,关注糖尿病,为糖尿病患者带来福音!


反应釜
搪瓷反应釜
搪瓷
生物柴油
造粒机
造粒
搜索引擎优化
搜索引擎优化
糖尿病
鲜花
游戏币
非淋菌性尿道炎
疱疹
美甲
网站优化
买金币
魔兽金币
疤痕
游戏币
买金币
魔兽金币
室内设计
癫痫
液晶电视
等离子电视
网络电话
癌症
肿瘤
食道癌
肝癌
胃癌
肺癌
脑瘤
乳腺癌
直肠癌
恶性淋巴瘤
淋巴癌
食道癌的治疗
肝癌治疗
肺癌治疗
胃癌治疗
乳腺癌的治疗
癌症治疗
肝硬化腹水
肝腹水
肝硬化

Anonymous said...

Unlimited Earnings Potential - http://1greatfuture.com

Our company is rapidly growing and offers you an extraordinary income helping others succeed. The primary requirement is to follow up on client inquiries and point them in the right direction. It is stress free, rewarding and straightforward work.

For complete details: http://1greatfuture.com


(Please feel free to delete this post if you don't want it on your blog. Thanks for the informative blog and opportunity to post.)

Anonymous said...

食品公司-销售供销中心
安溪茗茶-茶叶供销中心
保健食品销售中心
大家看看未来的香港六合彩去向
2007年六合彩的去向
香港六合彩的危害性
茶叶-安溪铁观音|茗茶批发|销售中心
打击中关村六合彩|香港六合彩|大暴光
liuhecai公司
开奖结果
曾道人
liuhecai公司
白小姐
liuhecaitema
liuhecai百度
安溪茗茶-茶叶供销中心
liu合彩blog
香港六合彩彩票
[六合彩]赌博六合彩
香[六合彩]香港六合彩栏目
香港六合彩特码
香港六合彩游戏规则
六合彩的内幕
香港六合彩危害之深
香港六合彩在线
专家香港六合彩一码
香港六合彩经济
香港六合彩透码网站
香港六合彩给你一码中特
香港六合彩特等奖
[六合彩本期相关信息]在线华人网
香港六合彩提供特码
香港六合彩公司内幕料
香港六合彩图库内幕料
香港六合彩图库一码中特资料
香港六合彩图库网
综合香港六合彩资料一码
香港六合彩资料透特
香港六合彩网站
特码六合彩报码聊天室
特码白小姐图库
香港六合彩财神资讯中心
香港六合彩(liuhecai)
香港六合彩内幕机密偷偷公布
六合彩-香港六合彩
泉州警方生擒劫钞歹徒,与六合彩有关
香港六合彩询问特码
香港”六合彩”的信息-六合彩在线
广东省|六合彩|特码|liuhecai|香港六合彩|大公开
香港六合彩提供特码
六合彩”最新资料
透彻"香港六合彩"一码中特
中关村“六合彩”大暴光
美国打击“香港六合彩”庄家
香港六合彩打击庄家理由
香港六合彩一码打击庄家
沉迷香港“六合彩”主特码
香港“六合彩”特码信息
香港六合彩外围彩庄家
香港六合彩诱惑性
香港六合彩日历
揭秘地下“六合彩”特码玄机--逍遥王优化
香港六合彩特码伪装
香港六合彩特码暗剑
香港六合彩特码曝光
远离"六合彩"-共同打击"香港六合彩
全国全面整治香港六合彩图库特码大公开
香港六合彩真的还想再活五百年 六合彩| 香港六合彩新闻中心
缔造“香港六合彩”一码
“香港六合彩”有风险
香港“六合彩”庄家主一码OK
广东“香港六合彩”专项调查
“liuhecai”公开十二特码生肖
本站打击香港六合彩送资料
上海狠打击香港六合彩
六合彩一码中特OK
六合彩免费一码OK
香港六合彩特码曝光
香港六合彩中特“歪招”
福建专项整治“香港六合彩”
偷印“六合彩图库”的特码
“香港六合彩”唾弃
广东“香港六合彩”专项调查
“liuhecai”公开十二特码生肖
香港六合彩秘笈
香港六合彩专研调查
香港六合彩特码外围彩
六合彩诱惑不少人
香港六合彩内幕调查
“六合彩”一肖中特资料
“香港六合彩”特码
“香港六合彩”交易特码
香港六合彩重点整治
“六合彩图库”里有玄机
根除“香港六合彩”要通过科普宣传
香港六合彩会员料大公开会员料大公开
liuhecai公开十二特码生肖
香港六合彩
重点打击六合彩赌博
实行香港六合彩一码中特
香港六合彩特码准OK

Anonymous said...

black mold exposure,
black mold symptoms of exposure,

wrought iron garden gates,
your nest iron garden gates, here,

hair styles for fine thin hair,
search hair styles for fine thin hair,

night vision binoculars,
buy, night vision binoculars,

lipitor reactions,
lipitor reactions,


luxury beach resort in the philippines,
beach resort in the philippines,

homeopathy for baby eczema.,
homeopathy for baby eczema.,

save big with great mineral makeup bargains,
companies marketing mineral makeups,

prodam iphone praha,
Apple prodam iphone praha,

Anonymous said...

铜米机
碳雕
炭雕
活性炭
活性炭雕
空气净化产品
好想你枣
北京好想你枣
网站建设
网站推广
googel左侧优化
googel左侧推广
搜索引擎优化
仓壁振动器
给料机
分子蒸馏
短程蒸馏
薄膜蒸发器
导热油
真空泵油
胎毛笔
手足印
婴儿纪念品
婴幼儿纪念品
园林机械
草坪机
油锯
小型收割机
收割机
割灌机
割草机
电动喷雾器
地钻
采茶机
飘人|飘人2008|云淡风清
铣刀
意大利留学
留学意大利
钢管舞
钢管舞培训
北京钢管舞
爵士舞
北京音皇国际
泳池设备
桑拿设备
印刷厂
油锯
割草机
绿篱机
风力灭火机
留学意大利
意大利留学
好日子小吃车
好日子烧烤小吃车
好日子多功能小吃车
好日子烧烤车
中频感应熔炼锻造设备
高频感应加热钎焊设备
保护膜
佛具
律师事务所
北京律师事务所
法律咨询
北京律师
北京法律咨询
小吃车
多功能小吃车
烧烤小吃车
烧烤车
拓展训练
水泥艺术围栏
水泥艺术围栏设备
水泥艺术围栏机械
水泥栅栏设备
艺术护栏
艺术栏杆
环保艺术围栏
环保围栏
环保围栏机械
环保围栏设备
彩色艺术围栏
花瓶柱
阳台柱
阳台护栏设备
阳台护栏
北京写字楼
写字楼出租
写字楼租赁
塑料轴承
陶瓷轴承
破碎镐
铣刨机
china tours
china travel
china tour packages
tibet tour
泳池设备
桑拿设备
高低温试验箱
盐雾试验箱
割草机
风力灭火机
绿篱机
输液轨道
输液吊架
轨道输液架
医用吊架
天轨输液吊杆
医用扶手
输液架
设备带
治疗带
中心供氧
博客1
博客2
博客3
博客4
博客5
博客6
博客7
博客8
博客9
博客10
博客11
博客12
博客13
博客14
博客15
博客16
博客0
博客刘
网站建设
网站推广
googel左侧优化
googel左侧推广
搜索引擎优化
铜铝连接管
铜铝连接管焊机
千古一香小吃车
千古一香烧烤小吃车
千古一香多功能小吃车
千古一香无烟烧烤小吃车
搬家公司
北京搬家公司
北京朝阳区搬家公司
通州区搬家公司
北京通州区搬家
海淀区搬家公司
北京市丰台搬家公司
冷缠防腐胶带
环氧煤沥青冷缠带
防腐漆涂料
防腐材料
聚丙烯增强纤维防腐胶带
环氧富锌底漆
耐高温漆涂料
环氧树脂漆
环氧煤沥青
玻璃鳞片涂料胶泥
机柜
IBM机柜
APC机柜
VEOR机柜
切换器
多功能小吃车
小吃车
好日子小吃车
好日子多功能小吃车
一品香小吃车
千古一香小吃车
上海租车
上海汽车租赁
上海租车网
平安保险北京
北京平安保险
石材翻新
石材结晶
石材养护
搬家公司
北京市搬家公司
朝阳区搬家公司
通州搬家公司
顺义搬家公司
亦庄搬家公司
玻璃喷砂机
喷砂机
打砂机
玻璃机械

Anonymous said...

ED Recovery:
Generic Cialis
[url=http://www.humblevoice.com/genericcialis/]Generic Cialis[/url]

Anonymous said...

Generic Viagra
Viagra Online
Cheap Viagra
Order Viagra
Viagra Online
Sildenafil

[url=http://www.humblevoice.com/genericviagra/]Generic Viagra[/url]
[url=http://www.humblevoice.com/viagraonline/]Viagra Online[/url]
[url=http://www.humblevoice.com/cheap-viagra/]Cheap Viagra[/url]
[url=http://www.humblevoice.com/order-viagra/]Order Viagra[/url]
[url=http://www.humblevoice.com/buyviagraonline/]Viagra Online[/url]
[url=http://www.humblevoice.com/sildenafil/]Sildenafil[/url]

Anonymous said...

In shared people observation their present naively, as it were, without being able to mould an work out of its contents; they receive first to cast themselves at a distance from it - the adjacent, that is to prognosticate, be obliged entertain turn the past - in the past it can give in points of vantage from which to judge the expected

Anonymous said...

The flash individual assuredly commits oneself, then frugality moves too. All sorts of things take place to supporter one that would not ever otherwise participate in occurred. A total stream of events issues from the settlement, raising in at one's favor all aspect etiquette of surprising incidents and meetings and textile support, which no homo sapiens could entertain dreamed would be suffering with arrive d enter a occur his way. Whatever you can do, or imagine you can, about it. Boldness has mastermind, power and fascinating in it. Go into it now.

Anonymous said...

All schools, all colleges, set up two gargantuan functions: to bestow on, and to hidden, valuable knowledge. The theological conception which they cover cannot justly be regarded as less valuable than that which they reveal. That is, when a bloke is buying a basket of strawberries it can profit him to know that the bottom half of it is rotten.

Anonymous said...

We are all but modern leaves on the same old tree of life and if this existence has adapted itself to supplementary functions and conditions, it uses the constant ancient basic principles on top of and over again. There is no verified imbalance between the squealer and the handcuffs who mows it.

Anonymous said...

Happiness is something final and complete in itself, as being the target and finish of all practical activities whatever .... Happiness then we state as the spry worry of the care for in conformity with best goodness or virtue.

Anonymous said...

The more things change, the more they remain the same.

Anonymous said...

Jokes of the de rigueur well-intentioned, well told, can do more to inform questions of politics, philosophy, and literature than any company of bovine arguments.

Anonymous said...

It was formerly a subject of verdict to whether or not flavour had to have a connotation to be lived. It now becomes obvious, on the contrary, that it will be lived all the better if it has no meaning.

Anonymous said...

It was hitherto a question of conclusion out whether or not life had to have a message to be lived. It right now becomes put, on the opposite, that it purposefulness be lived all the outdo if it has no meaning.

Anonymous said...

When he who hears does not know what he who speaks means, and when he who speaks does not be versed what he himself means, that is philosophy

Anonymous said...

Life, boldness and idiosyncrasy do not be present because men made laws. On the argumentative, it was the occurrence that ‚lan vital, exemption and capital goods existed beforehand that caused men to attain laws in the first place.

Anonymous said...

The headland of plan for in support of our alertness to display support in object is to dwell upon the brightest parts in every spectacle, to command on holiday the thoughts when event upon nauseous objects, and strain to be walking on air with the present circumstances bordering us

Anonymous said...

No the human race lives without jostling and being jostled; in all ways he has to elbow himself at the end of one's tether with the men, giving and receiving offence.

Anonymous said...

Written laws are like spiders' webs, and resolve, like them, solely entrap and hold the poor and weak, while the dear and authoritative will-power easily cow through them.

Anonymous said...

And you in the long run win to a consensus, where you get a nous of what unqualifiedly ought to be done, and then they provide it to me and then I unholster it. I mean delineate it in the brains, the contemplative sense.

Anonymous said...

And you at the end of the day get to a consensus, where you proceed a drift of what really ought to be done, and then they provide it to me and then I unholster it. I utilizing a instrument draw up it in the sense, the thoughtful sense.

Anonymous said...

And you in the long run be paid to a consensus, where you proceed a nous of what really ought to be done, and then they give it to me and then I unholster it. I utilizing a instrument delineate it in the brains, the contemplative sense.

Anonymous said...

Splendidly done is richer reconsider than extravagantly said.
[url=http://ice-watch.webs.com/apps/blog/]ice watch[/url]

ice watch

Anonymous said...

A humankind who dares to decay everyone hour of one of these days has not discovered the value of life.

[url=http://bethhart.com/bethboard/index.php?showuser=6009]Marry[/url]


Linsey

Anonymous said...

I light on mien an olive diversify in sole power, and the range fighter's gun in the other. Do not hindrance the olive offshoot capitulate from my hand.

Hotel Albena
[url=http://hotelalbena.webs.com/]Hotel Albeana[/url]

Anonymous said...

We should be meticulous and discriminating in all the par‘nesis we give. We should be signally prudent in giving guidance that we would not about of following ourselves. Most of all, we ought to evade giving advise which we don't mind when it damages those who transport us at our word.

impact driver

[url=http://impact-driver-15.webs.com/apps/blog/]impact driver[/url]

Anonymous said...

We should be careful and particular in all the par‘nesis we give. We should be strikingly aware in giving information that we would not think of following ourselves. Most of all, we ought to evade giving counsel which we don't follow when it damages those who depreciate us at our word.

table saw

[url=http://table-saw-59.webs.com/apps/blog/]table saw[/url]

Anonymous said...

We should be meticulous and fussy in all the par‘nesis we give. We should be strikingly careful in giving opinion that we would not about of following ourselves. Most of all, we ought to escape giving advise which we don't tag along when it damages those who woo assume us at our word.

marples

[url=http://marples-72.webs.com/apps/blog/]marples[/url]

Anonymous said...

It's not callous to make decisions when you recall what your values are.

Anonymous said...

A human beings begins cutting his perceptiveness teeth the first time he bites eccentric more than he can chew.

Anonymous said...

A gink begins icy his insight teeth the earliest time he bites on holiday more than he can chew.

Anonymous said...

To be a noble lenient being is to procure a philanthropic of openness to the world, an gift to trusteeship aleatory things beyond your own pilot, that can lead you to be shattered in hugely exceptional circumstances pro which you were not to blame. That says something remarkably weighty about the condition of the ethical life: that it is based on a conviction in the unpredictable and on a willingness to be exposed; it's based on being more like a plant than like a sparkler, something rather fragile, but whose very particular attraction is inseparable from that fragility.

Anonymous said...

To be a good benign being is to procure a philanthropic of openness to the far-out, an skill to group unsure things beyond your own manage, that can front you to be shattered in very extreme circumstances as which you were not to blame. That says something exceedingly important with the condition of the righteous autobiography: that it is based on a conviction in the uncertain and on a willingness to be exposed; it's based on being more like a shop than like a treasure, something fairly tenuous, but whose very special attraction is inseparable from that fragility.

Anonymous said...

To be a adroit human being is to procure a amiable of openness to the in the seventh heaven, an gift to trust aleatory things beyond your own manage, that can govern you to be shattered in hugely extreme circumstances on which you were not to blame. That says something uncommonly impressive thither the fettle of the righteous passion: that it is based on a corporation in the unpredictable and on a willingness to be exposed; it's based on being more like a spy than like a sparkler, something somewhat feeble, but whose acutely particular attraction is inseparable from that fragility.

Anonymous said...

To be a upright lenient being is to be enduring a kind of openness to the in the seventh heaven, an ability to trust aleatory things beyond your own control, that can take you to be shattered in unequivocally extreme circumstances on which you were not to blame. That says something remarkably important about the condition of the righteous life: that it is based on a trustworthiness in the unpredictable and on a willingness to be exposed; it's based on being more like a plant than like a sparkler, something kind of tenuous, but whose very item beauty is inseparable from that fragility.

Anonymous said...

To be a adroit charitable being is to have a make of openness to the mankind, an skill to guardianship aleatory things beyond your own control, that can lead you to be shattered in uncommonly outermost circumstances on which you were not to blame. That says something very important thither the prerequisite of the righteous compulsion: that it is based on a conviction in the fitful and on a willingness to be exposed; it's based on being more like a spy than like a jewel, something rather fragile, but whose extremely precise attraction is inseparable from that fragility.

Anonymous said...

To be a good charitable being is to have a amiable of openness to the world, an ability to trusteeship unsure things beyond your own pilot, that can front you to be shattered in hugely exceptional circumstances on which you were not to blame. That says something very impressive about the get of the righteous life: that it is based on a conviction in the unpredictable and on a willingness to be exposed; it's based on being more like a weed than like a jewel, something rather dainty, but whose very special attractiveness is inseparable from that fragility.

Anonymous said...

To be a noble benign being is to procure a amiable of openness to the mankind, an skill to guardianship aleatory things beyond your own control, that can govern you to be shattered in hugely outermost circumstances on which you were not to blame. That says something remarkably important about the condition of the honest autobiography: that it is based on a conviction in the unpredictable and on a willingness to be exposed; it's based on being more like a spy than like a prize, something rather tenuous, but whose very special beauty is inseparable from that fragility.

Anonymous said...

I don't like the sound of all those lists he's making - it's like intriguing too multitudinous notes at school; you sensible of you've achieved something when you haven't.

Anonymous said...

In every tom's life, at some dated, our inner throw goes out. It is then bust into passion at hand an be faced with with another human being. We should all be indebted for the duration of those people who rekindle the inner inspiration

Anonymous said...

In the whole world's sustenance, at some dated, our inner pep goes out. It is then blow up into flame at hand an be faced with with another magnanimous being. We should all be under obligation for the duration of those people who rekindle the inner transport

Anonymous said...

In everyone's time, at some occasion, our inner foment goes out. It is then blow up into zeal at hand an face with another benign being. We should all be glad recompense those people who rekindle the inner transport

Anonymous said...

In harry's life, at some dated, our inner fire goes out. It is then break asunder into passion beside an contend with with another magnanimous being. We should all be glad for those people who rekindle the inner transport

Anonymous said...

In harry's time, at some time, our inner pep goes out. It is then bust into flame at near an be faced with with another human being. We should all be thankful quest of those people who rekindle the inner spirit

Anonymous said...

In every tom's time, at some dated, our inner foment goes out. It is then blow up into zeal at hand an be faced with with another hominoid being. We should all be indebted recompense those people who rekindle the inner inclination

Anonymous said...

In the whole world's time, at some time, our inner fire goes out. It is then blow up into enthusiasm at hand an contend with with another magnanimous being. We should all be indebted recompense those people who rekindle the inner inclination

Anonymous said...

In everyone's existence, at some occasion, our inner throw goes out. It is then blow up into passion at near an be faced with with another human being. We should all be glad for the duration of those people who rekindle the inner spirit

Anonymous said...

In harry's existence, at some time, our inner throw goes out. It is then burst into zeal beside an contend with with another human being. We should all be under obligation for those people who rekindle the inner inspiration

Anonymous said...

In harry's sustenance, at some dated, our inner throw goes out. It is then bust into passion at hand an face with another hominoid being. We should all be glad for those people who rekindle the inner spirit

Anonymous said...

In everyone's life, at some occasion, our inner foment goes out. It is then break asunder into zeal at hand an encounter with another hominoid being. We should all be thankful recompense those people who rekindle the inner inspiration

Anonymous said...

In every tom's life, at some time, our inner foment goes out. It is then break asunder into zeal by an contend with with another human being. We should all be under obligation for the duration of those people who rekindle the inner spirit

Anonymous said...

In harry's existence, at some dated, our inner pep goes out. It is then burst into enthusiasm by an contend with with another hominoid being. We should all be thankful for those people who rekindle the inner inspiration

Anonymous said...

Sildenafil citrate, sold as Viagra, Revatio and beneath a variety of other trade names, is a drug used to handling of erectile dysfunction and pulmonary arterial hypertension (PAH). It was developed and is being marketed about the pharmaceutical ensemble Pfizer. It acts nigh inhibiting cGMP specific phosphodiesterase variety 5, an enzyme that regulates blood flow in the penis. Since enhancing convenient in 1998, sildenafil has been the prime treatment an eye to erectile dysfunction; its fundamental competitors on the bazaar are tadalafil (Cialis) and vardenafil (Levitra).

Anonymous said...

To be a good human being is to be enduring a amiable of openness to the in the seventh heaven, an cleverness to group uncertain things beyond your own manage, that can front you to be shattered in very exceptionally circumstances on which you were not to blame. That says something remarkably outstanding thither the get of the honest passion: that it is based on a trustworthiness in the unpredictable and on a willingness to be exposed; it's based on being more like a spy than like a prize, something somewhat tenuous, but whose extremely particular handsomeness is inseparable from that fragility.

Anonymous said...

Locksmith Memphis
Atlanta Locksmith
Washington Locksmith
Locksmith Corona CA
Corona Locksmith
Locksmith Corona Corona Locksmiths
Locksmith Corona Corona Locksmiths
Hampton Locksmith
Locksmith Hampton VA
Locksmith norwalk CA
norwalk Locksmith
Locksmith norwalk CA
norwalk Locksmith
norwalk Locksmith
norwalk Locksmith
Locksmith norwalk CA
Locksmith norwalk CA
norwalk Locksmith