Wicked Good Ruby, you going?

Thanks to the generosity of the folks over at Thoughtbot, the upcoming Wicked Good Ruby conference in Boston is proud to announce that childcare is being offered through Parents in a Pinch. Please contact me at liana at liana dot org if you are interested.

In addition, Jumpstart Labs is offering a Refactoring Rails Workshop to be held on Friday before the conference. And afterwards, I hope to see you at the Wicked Good Lightening Talks. If you are interested in speaking please contact brian at dockyard dot com

Wicked Good Ruby has only 50 tickets left, so now is the time to sign up!

Add comment September 15th, 2013

AutosaveAssociation

Recently, I wanted to utilize the AutosaveAssociation module which (wrapped in a transaction) autosaves a model record when the parent is saved. I’ll use the example from the api docs:

post = Post.new(title: 'ruby rocks')
 post.comments.build(body: 'hello world')
post.save # => saves both post and comment

But because I had a validation on comments that requires presence of post, it was attempting to validate comments before post was created. So instead of saving all my models in one swoop, it threw a validation error.

Wut? This should just work! I fussed with it for hours and the only workaround appeared to be to call save with validation: false.

Ew. Anyone else feel queasy?

Turns out the missing piece was to add inverse_of on my model associations. Here’s an awesome explanation from my coworker @mdaubs83:

The :inverse_of option is needed here to inform AR about the inverse association so that the Comment instance returned from build() can reference the original Post instance. This in turn allows the Comment instance to see the id of the Post instance after it’s saved and update it’s foreign key accordingly when AutosaveAssociation calls save() on associated objects. There are other benefits to using :inverse_of, we should probably consider adding the option to all associations. Here’s evidence of the issue and how adding inverse_of solves it:

 post.comments.build(user_id: user.id)

# has_many :comments
 post.comments.first.post.object_id == post.object_id
 # => false

# has_many :comments, :inverse_of => :post
 post.comments.first.post.object_id == post.object_id
 # => true

See also “Bi-directional associations” in Rails API Docs

Thanks, Matt! Sadly, inverse_of is never mentioned in the AutosaveAssociation docs. But I noticed this line in the ActiveRecord Association docs.

If you are using a belongs_to on the join model, it is a good idea to set the :inverse_of option on the belongs_to, which will mean that the following example works correctly (where tags is a has_many :through association):

@post = Post.first
@tag = @post.tags.build :name => "ruby"
@tag.save

The last line ought to save the through record (a Taggable). This will only work if the :inverse_of is set:

class Taggable ActiveRecord::Base
  belongs_to :post
  belongs_to :tag, :inverse_of => :taggings
end

Err. Okay. Would have been useful to see that little tidbit included in the AutosaveAssociation docs as well!

But good news! Matt also tells me that Rails 4.1 is poised to support automatic inverse_of detection. So once we all upgrade we get inverse_of goodness for free. W00t!

Commit d6b03a3 to rails/rails by wangjohn
https://github.com/rails/rails/commit/d6b03a376787ec9c9e934e5688a38c576f2e39b7

Add comment June 26th, 2013

Y U No Validate Me?

I have a class that inherits from Active::Model that is encapsulating some logic from other models. Problem is, I’d really like my object to display validation errors. All of them.

There are attributes on my ActiveModel class, and then I need to validate attributes on the ActiveRecord classes that are being updated here as well.

Yeah, I’m pretty sure that I’m not the only one who initially thought that self.errors returns an array of errors. It doesn’t. It returns the Errors object.

If you want to add to your errors object, turns out you gotta get friendly with Validations too. They go hand in hand.

It’s in the valid? method where all the magic happens. Check this out:

  def valid?(context = nil)
    super
    my_model.errors.each{ |k, v| self.errors[k] << v } if my_model.invalid?
    errors.empty?
  end

Ah, sweet validation.

Add comment June 20th, 2013

Tip of the Day

I’ve only ever used Date.today and Time.now to get the current date and time. But for those of us concerned with global timezones (and who isn’t), we should really be using the .current method.

Date.current is a rails core extension that gets the current date in the set time zone.

Awesomesauce.

Add comment June 19th, 2013

Favorites

Sometimes, I wanna set more than one var at once. This is one of my very favorite ruby tricks that I use every day:


irb(main):007:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):008:0> x, y, z = a.collect{|i| i * 2 }
=> [2, 4, 6]
irb(main):009:0> x
=> 2
irb(main):010:0> y
=> 4
irb(main):011:0> z
=> 6

Add comment May 23rd, 2013

The Artists Way

Is comparing software engineering to a career as an artist way off base? What does the term ‘disruptive innovation’ mean to you?

One of the basic tenants of wisdom in The Artists Way is to take good care of yourself.  This book is known in the art community as a guide toward unlocking creativity.  I have seen these same strategies espoused when folks discuss how to enhance productivity.

What the hell does becoming a more creative person have to do with productivity and becoming a better programmer?  I guess that depends upon your definition of ‘better’.

No one expects a composer to sit at the piano 60 hours a week churning out symphonies. Prolific writers don’t sit at their desks for endless hours either. The idea is ridiculous.

Well documented is the fact that productivity goes down when the work week exceeds 40 hours. Programming is hard mental work and sitting for prolonged periods of time doesn’t help either.

I have spent the last few years exhausting myself trying to “level up” as a rubyist. I can honestly say that my best work and ah-ha! moments occurred when I was awake and refreshed. And when my mentors were awake and refreshed as well.

One of my favorite exercises from the book is The Artist’s Date. It’s a once-weekly, festive, solo expedition to explore something that interests you to spark whimsy and encourage play.

How many times have you solved that vexing problem the moment you stepped away from your desk? When do your best solutions pop into your brain? When you are tired, beat down and depressed?

Thanks to Luminosity, I have first hand, tangible evidence that my brain improves with a regular diet of sleep, creativity and connection with my family.

This is the part where I share my personal brain performance index starting from a deeply stressful, sleepless period in my life to a much happier place. These are sans numbers of course but you get the idea…

BMI

This success is due to a strong effort on my part to truly seek out work life balance. I’m a better mom and a better programmer today. What steps have you taken in your life to not only become a better and more productive programmer, but to become a better YOU?

Add comment March 24th, 2013

Bottom line, We All Lose

Here’s an excellent post by Amanda Blum discussing the latest Women in Tech tragedy. I call it a tragedy because people got hurt and damage could have been avoided every step of the way but instead spiraled out of control.

Standing up about women’s issues is hard. Choosing which issues are worthy of fighting the good fight and how best to handle a situation are not always obvious. It’s a touchy subject. Folks are nervous.

Sometimes the pendulum has to swing too far in the other direction before things right themselves. Was her public response an overreaction? Christie Koehler makes a good point in a recent post Bold IdeasUttered Publicly: “…as someone not part of the dominant social order you have limited options for calling attention to transgressive behavior.”

Read the entire post for a thoughtful explanation of what that means, but my take (and from personal experience) is that it’s really, really hard for a minority to speak up directly.

Let’s be honest, when was the last time you called someone out for bad behavior to their face?

Regardless of whether you side with Adria or with how she handled the incident, all can agree that things definitely got out of hand thereafter.

Honestly, I feel bad that someone was fired over something that could easily have come out of my own mouth but it’s the subsequent nerd range and death threats against Adria that make me even more afraid to speak up (about anything) than I was before.

As a dear male colleague said to me, “Why can’t we talk openly about these things without it exploding?”

1 comment March 22nd, 2013

Sarcastic Catchphrase… Not!

I can already tell that .not is going to be my new favorite thing in rails 4.

i.e.
Article.where.not(name: "Hello")

That is all.

Add comment March 19th, 2013

Holiday Side Project

Since I’ve been wanting to try out CanCan and Twitter Bootstrap this holiday, I spent a few hours creating a silly game called Recycled or Not.

My app was generated with Rails Apps Composer. The rails_apps_composer gem installs a command line tool to assemble a Rails application from a collection of fully-tested “recipes.”

It’s too bad the core recipes don’t include MiniTest, although this is a option when creating recipes from scratch. And the core example recipes use Cucumber which has fallen out of fashion around here of late. But other than that, I really loved using this tool because it allowed me to get started with the features of my project in a snap.

In addition, I enjoyed working with Devise, Rolify and CarrierWave. But I think my favorite discovery was that the load_resource method from Cancan will set up a before filter in your controllers which loads the model resource into an instance variable for you. Love that!

Add comment January 1st, 2013

TechiesGotTalent

Last night, I had the privilege of being part of the first annual TechiesGotTalent night to showcase the extracurricular activities of local nerds here in Boston.

Kelly Rice did an amazing job of gathering talent from the Boston Startup scene and I was really impressed with the quality of all the acts that performed.

Checkout my 5 minutes with Peter Fernandez here: http://www.youtube.com/watch?v=4vxOwsMtU…

We won third place!

Add comment November 29th, 2012

Previous Posts


Pages

RSS Tweets

RSS Berkman Gender & Tech

Links

Tags

Meta