Oh Geek Mama… Why o why have you forsaken the Railscasts project? Is it your homage to _why last week?
No. I haven’t given up! I swear I haven’t! Big project at work. Working all weekend. Up until wee hours. Exhausted.
But I won’t let that stop me. Just slow me a down a little bit. But this week I’m getting myself back on track…. just in time for my planned vacation at the end of the month. Yay.
Ain’t nothing gonna break my stride
Nobody’s gonna slow me down
Oh no, I’ve got to keep on moving
Ain’t nothing gonna break my stride
I’m running and I won’t touch ground
Oh no, I’ve got to keep on moving
Yes. I admit it. I wanted to grow up to be a Solid Gold dancer.
August 23rd, 2010
Okay, people. I want a show of hands from everyone is Looping Through Flash messages as shown in episode #18:
<% flash.each do |key, msg| %>
<%= content_tag :div, msg, :id => key %>
<% end %>
Instead of simply using the scaffold default which looks like this:
<% unless flash[:error].nil? %>
<div id="error"><%= flash[:error] %></div>
<% end %>
<% unless flash[:notice].nil? %>
<div id="notice"><%= flash[:notice] %></div>
<% end %>
I can’t say that I’ve ever seen Ryan’s example in the wild. In fact, I’m not even ashamed to admit that I”ve never seen anyone use the content_tag to create div tags in a view. Have you?
Admit it. You’ve forgotten all about content_tag. Damn, I’m glad I didn’t skip these early episodes!
August 17th, 2010
Good workout. Thanks for asking. Felt so yummy to be back in the gym. Wish my husband didn’t have evening clients so I could do it more often.
Unfortunately, my husband’s sporadic schedule combined with my kids’ relentless bedtime ritual means it’s impossible for me to keep a regimen going. Which is really ironic when you consider that he’s a personal trainer.
Anyway, back to rails related stuff…. I am freaking amazed how I learn something new with each episode. Tonight, I watched episode #17: HABTM (Has And Belongs To Many) Checkboxes.
Did you know that simply passing an array of values to your foreign key will auto-populate your join table? Ryan’s example illustrates this nicely via script/console:
>> p = Product.find(:first)
>> p.category_ids = [2, 3]
This code above automatically created the SQL insert statement below:
INSERT INTO categories_products('product_id', 'category_id')
VALUES(1, 2)
Of course this only works with HABTM. You’re outta luck with Has Many Through and gotta roll your own. Still, this little shortcut is kinda nifty.
By the way… don’t you just HATE that empty checkboxes return NOTHING in HTML. In other words, when the checkbox is not checked no value is sent.
What a PAIN IN THE ASS! Sure there are ways to get around this, but what genius thought this up?
Yeah okay, there’s most likely some serious reason why this must be… but all I know is that I have to jump through stupid hoops and make an extra database call to get this to work properly. LAME!
August 16th, 2010
Okay… I got behind over the weekend. I skipped a couple of days and now I have to do double posts to keep up with my end goal.
But… I’ve also got to get to the gym. No really. I’ve got this wedding coming up and I think there’s a possibility that my ex-boyfriend who I haven’t seen in 15 years is showing up. Yeah.
After two kids it’s especially important to look sexy for this event… even though I think it might be a Star Treck/ Zombie theme.
Actually, I’m not sure if the groom is kidding about this or not. I’m hoping we do end up in costume. Although, honestly ladies trekkie costumes tend to be a little unforgiving.
Anyway, both endeavors are incredibly important. So I’ve really got to stay motivated. Thankfully, your feedback has been incredibly helpful. I’ve gotten some really great emails and comments from folks and I want to let you know that I really appreciate it.
Okay… gym now. Second post later. And if my dear friend does have a costumed wedding, I promise to bring mine to RailsConf!
August 16th, 2010
In Episode 16: Virtual Attributes, Ryan shows us how to use virtual attributes to finess first and last name fields on a form. Of course, this is a very simplistic example. What about middle initials? Suffixes? Prefixes?
Every single time I try to use this feature, I find that usually there’s a very good reason these fields are separated in the database. Because mushing things together gets too complex when you start considering the edge cases.
Consider addresses or phone numbers. They’re straightforward enough until you start thinking globally.
I dunno. I’m sure there are brilliant examples out there of ways this can be used… but I’d like to see something a little slicker than just concatenating a name. got any examples to share?
August 16th, 2010
I have always, always written out my find conditions the long way:
Post.find(:first,
:conditions => ['status = ? and active = ?', 1, 1])
And in some more complicated joins, this is entirely appropriate. But in Episode 15: Fun with Find Conditions, I’ve learned that you can specify conditions via a ruby hash:
Post.find(:first,
:conditions => { :status => 1, :active => 1 })
While the long way works just fine, what’s amazing to me is that this has been available since Rails 1.2. Where the hell have I been? Oh, yeah…. having two babies… but STILL!!
Fortunately, Ryan’s got my back and I know (because I’ve been skipping ahead) that in Rails 3 the game changes and ActiveRecord provides a new interface for performing database queries.
Instead of :conditions we now use the where method, passing in the same arguments as we would to :conditions.
But whoa… let’s slow down there, Partner! Baby steps. Tortoise and the hare and all.
August 11th, 2010
In the past, I avoided Performing Calculations on Models: episode 14 because I thought that under the hood rails was simply iterating through the array of active records returned from your sql query.
Turns out I was wrong.
There’s a much easier way than this to utilize the SQL functions and get the performance you want:
>> Task.find(:first,
:select => 'SUM(priority)'
:conditions => 'complete=0')
In this episode, Ryan shows us the sql generated from his queries in the script/console and this:
>> Task.sum(:priority,
:conditions => 'complete=0')
creates the desirable sql query below:
SELECT sum(priority) AS sum_priority
FROM tasks
WHERE (complete=0)
And of course this means that these methods are available through associations. For example:
>> Project.find(:first).tasks.sum(:priority,
:conditions => 'complete=0')
is turned into this:
SELECT sum(priority) AS sum_priority
FROM tasks
WHERE (tasks.project_id = 1) AND (complete=0)
What SQL functions are available? Checkout the Calculations class in the api.
August 10th, 2010
Is it just a coincidence that Episode 13 is fraught with danger?
In Dangers of Model in Session, Ryan devotes a whole episode to remind us not to store complex objects in our session.
Objects that are okay to store are very simple objects such as arrays, and strings, and ids, and hashes, and integers. But complex objects like models should be kept inside the database where the results are fetched every time.
There were a couple of comments on this post where folks noted performance problems as a reason to persist an object, but Ryan answers:
If you need a performance boost, look into a memcache solution.
But Ryan, I have to wait till episode #115!
August 9th, 2010
Since the weekend is upon us and the next few railscasts are a three parter, I’ve decided to combine episodes 10, 11 and 12 into this one blog post. I think that’s fair, don’t you?
Besides, I’m headed to the beach with my family on Sunday. And the ocean with two toddlers means all hands on deck! It’s all about the kids, people… and the vodka… after they’re asleep in bed.
Ryan says:
Refactoring is improving the design of code without changing its functionality.
In episode 10, we can all agree that moving code from your view into the model is an obvious win. And I appreciated watching Ryan show us in episode 11 the value of using an array to append a string over a local variable. Really elegant.
It’s also nice to be reminded of .blank? which will return true if a string is either nil or empty.
But the most important takeaway is using testing to ensure that our zeal to refactor doesn’t break existing code.
Of course, we all test… don’t we? Riiiiight?
In fact, I believe that the rockstars do. But the rest of us folks are still doggedly trying to convince our boss of the ROI.
Perhaps your app is so big and so complex that you and/or your colleagues are afraid to refactor. Well, this is when testing becomes invaluable.
Never tested before? Don’t let that stop you. Lord knows I was really intimidated before I wrote my first test. And I know full well that I still have lots to learn. But where do you start?
What’s the difference between a unit and functional test? What about RSpec and Cucumber and all those tools and frameworks that make my head spin?
Let me be the first to virtually take hold of your hand and whisper, “It’s all gonna be okay.”
Just start with Unit Test. It comes for free with every rails app. Betcha you got a test directory in your app right now. Sure, it’s a little neglected but maybe it just needs a little love.
I got a blog post that’ll walk you through the basics. Chances are you’ve already seen the tutorials though and just need the confidence to get your feet wet.
Just pick a model and write a couple functional tests to start. Betcha you get hooked.
August 7th, 2010
O-M-G! Until episode #9: Filtering Sensitive Logs, I can’t believe that I’ve forgotten to worry about my log files. Which is pretty lame considering that I worked for StopBadware and discussed security issues on a daily basis.
Since SBW, I’ve been highly sensitive about encrypted passwords as well as browser and web host server software being up to date. Mostly because, it’s such an easy fix to thwart the casual hacker.
In fact, I get personally offended when I sign up for a web service and promptly receive an email displaying my registration info including my password in plaintext.
I mean, seriously people! Encrypted passwords are sooo last decade. We’ve moved on since then, haven’t we?
But holy obvious, Batman! Why did I forget to consider my logs files?!
Well, maybe because user authentication is done via popular gems these days like Clearance and Authlogic.
I just checked a side project I’ve been working on in which Authlogic is implemented and sure enough, filter_parameter_logging can be found in the Application Controller.
*Whew*! Crisis avoided.
THIS is why it’s likely best to let others with oodles of security experience handle authentication and authorization. Others will disagree, but I am very thankful that there are gems out there that let me concentrate on the other things that I’m good at and leave security management to the experts.
August 6th, 2010
Previous Posts