May 28, 2015 by Daniel P. Clark

Rails’ HashWithIndifferentAccess

A Ruby Hash is a very powerful collection type to use in Ruby.  When working in Rails sometimes you’ll have symbols as keys or sometimes they will be strings.  If you use a normal Hash these will store as different keys for the same name. sample = Hash.new sample[:a] = 1 sample[“a”] = 2 sample…

Continue Reading »

May 16, 2015 by Daniel P. Clark

Switching From Unicorn to Puma on Heroku is Easy!

I’ve had my project running on Heroku with Unicorn for some time now.  Not that long ago Heroku put out a notice advising people to switch over to Puma as Unicorn doesn’t do well with low latency connections.  And you know that with the internet you’re always going to have people with slow connections accessing…

Continue Reading »

May 15, 2015 by Daniel P. Clark

Speed Up Your Rails Views By Deferring Changes

Nobody likes a slow website.  And the more notifications and content privileges you calculate beforehand the longer your web requests take to load and send.  The most efficient way to change this is to make as much of your site static as you can and defer your changes to after the page has fully loaded. …

Continue Reading »

May 13, 2015 by Daniel P. Clark

Rails: Don’t “pluck” Unnecessarily

Using pluck in Rails is both quick and efficient.  But there are cases where it’s not what you want to use.  For example if you’re selecting all users who have contacts, you might see something like this. User.where(id: Contact.pluck(:user_id)) Rails’ where method allows for Arrays of items for lookup so the above code might turn…

Continue Reading »

May 12, 2015 by Daniel P. Clark

Fake a SMTP Server with Python for Rails Testing

So I’ve been in the process of writing an integration test to verify the flow of registration through some unique conditions.  One thing I ran into while writing my Rails test was this error. Connection refused – connect(2) for nil port 25 After looking around at documentation on what to do for testing it seemed…

Continue Reading »

May 11, 2015 by Daniel P. Clark

HABTM; You Don’t Need It

My point: “For any valid use of implementing a has_and_belongs_to_many there is an equally valid way to implement it without it.” A few reasons: Rails had changed both the way it worked and removed it from reflections for a brief period (Rails version 4.1.0 and 4.1.1).  Also the preferred implementation is to use has_many :through. …

Continue Reading »

May 9, 2015 by Daniel P. Clark

Chaining an Array into Arel ors

I’d like to keep this post short.  So the basic idea behind this is that Arel helps you build SQL queries.  When you want to join multiple queries together into one result you use Arel’s or method.  The way Arel chains additional query commands is by nesting inside the current query.  For example, if you…

Continue Reading »

May 2, 2015 by Daniel P. Clark

Private Module Methods in Ruby

So this is something I’ve looked into many times over the years and have never found an answer to.  But great news is that I’ve found a way! It’s as simple as defining module methods as private within the singleton class.  Here’s how I did it in my gem PolyBelongsTo module PolyBelongsTo::Core extend ActiveSupport::Concern included…

Continue Reading »

April 30, 2015 by Daniel P. Clark

Have problems testing methods that query ActiveRecord?

Sometimes you’ll run into a problem that won’t reveal an answer.  I ran into a problem I had come across before in that past to which I knew I hadn’t really solved.  I have a custom method that looks up an ActiveRecord query and makes a change.  But both times I’ve tried to test the…

Continue Reading »

April 16, 2015 by Daniel P. Clark

Undefining a CSS attribute (and rant)

Stack Overflow Conduct One downside to Stack Overflow is the abuse of power the higher ranking users exercise when they misunderstand the question posed.  Many are too quick to jump the gun on voting out or down questions rather than simply asking for clarification before action is taken.  S.O. has guidelines for people to do…

Continue Reading »

Page 4 of 11
1 2 3 4 5 6 11
1 2 3 4 5 6 11