ActsAsTaggable acts quirky
I’ve been recently working on a project with the extensive use of ActsAsTaggable gem (version 2.4.1). I would like to share a couple of things that were not immediately evident and caused some confusion.
Issue #1
You should be consistent with how you assign tags. ActsAsTaggable is very versatile as it provides such features as tagging contexts and tagging ownership. Contexts allow to create named subsets of tags for the item. Ownerships allow for the item to have different tags for different owners. It is very easy to assign tag in the default “tags” context and later be wondering why the tag is not showing up in the custom context, or vice versa. The following method always assigns the tag within the “tags” context:
@photo.tag_list = "New York, USA"
So if you update the tag list in the custom context later:
@photo.set_tag_list_on("album_3", "New York, USA")
you will basically be creating the second tagging for the “New York” tag with the “album_3” context.
The third tagging is generated if the same photo is tagged by the “owner” @user.
@user.tag(@photo, :with => "New York", :on => "album_3")
Issue #2
Tag count methods include the …
rails
RailsConf 2014 on Machine Learning
This year at RailsConf 2014, there are workshop tracks which are focused sessions double or triple the length of the normal talk. Today I attended Machine Learning for Fun and Profit by John Paul Ashenfelter. Some analytics tools are good at providing averages on data (e.g. Google Analytics), but averages don’t tell you a specific story or context of your users, which can be valuable and actionable. In his story-telling approach, John covered several stories for generating data via machine learning techniques in Ruby.
Make a Plan
First, one must formulate a plan or a goal for which to collect actionable data. More likely than not, the goal is to make money, and the hope is that machine learning can help you find actionable data to make more money! John walked through several use cases and examples code with machine learning and I’ll add a bit of ecommerce context to each story below.
Act 1: Describe your Users
First, John talked about a few tools used for describing your users. In the context of his story, he wanted to figure out what gender ratio of shirts to order for the company. He used the sexmachine gem, which is based on census data, to predict the sex of a person based on a …
conference rails machine-learning
Dictionary Comprehensions in Python
Python has many features which usually stay unknown to many programmers.
List Comprehensions
List comprehensions are much simpler way of creating lists. This is one feature which is rather widely used and I saw this in many examples and source of many libraries.
Imagine you have a function which returns a list of data. A good example of this is xrange(start, end) function which returns all numbers within the range [start, end), so it excludes the end. This is a generator, so it doesn’t return all numbers at once, but you need to call this function many times, and each time it returns the next number.
Getting all numbers from range [1, 10] using this function can be done like this:
numbers = []
for i in xrange(1, 10):
numbers.append(i)
If you want to get only the even numbers, then you can write:
numbers = []
for i in xrange(1, 11):
if i % 2 == 0:
numbers.append(i)
List comprehensions can make the code much simpler.
The whole expression evalutes to a list, and the main syntax is:
[ expression for item in list if conditional ]
The first example can be then written as:
numbers = [i for i in xrange(1, 11)]
and the second:
numbers = [i for i in xrange(1, 11) if i % 2 == 0] …
python
Rails Tips & Tricks at RailsConf 2014

Rails: They start so young!
One of the talks I attended on Day two of RailsConf 2014 was Tricks that Rails didn’t tell you about by Carlos Antonio. As the title suggests, Carlos covered a number of Rails items that are either not widely used or not well documented. I’ve taken the lazy approach and listed out all the topics he covered, but provided documentation or relevent links to all of the tricks in case you’d like to learn more.
Migrations
- Use change_column_null to change a column null, which is reversible in a migration.
- Use change_column_default to change a column default, but this doesn’t work with change (isn’t reversible).
ActiveRecord
- Relation.merge, e.g.
Product.joins(:reviews).merge(Review.approved)
, allows you to utilize scope from another model rather than passing in exact SQL conditional, i.e. limiting knowledge of Review to Product. - Utilize group counting to group results by a column and get the count. This also accepts multiple fields to group.
- relation.first! and relation.last! are similar to first & last but raise exception RecordNotFound if there are no records.
- where.not is a cool finder trick, e.g.
scope :some_scope
, ->{ where.not status: 'draft' } …
conference rails
Spree Security Update 2.x.x Error, undefined method ‘assume_from_symbol’ for Money:Class (ActionView::Template::Error)
On March 25, 2014 Spree Commerce posted an announcement that there was a security vulnerability for all Spree 2.x.x versions. As reported on Spree’s website,
“The exploit would require the attacker to randomly guess valid order numbers, but once achieved, the technique would reveal private customer information associated with the order. Credit card details are never stored in Spree and were never at risk by this exploit.”
So, this update is pretty typical and as usual, you should make sure nothing has broken with any changes that were made. I wanted to note a couple of “gotchas” with this most recent update however.
The first of which is probably obvious and really more of a reminder as sometimes people forget. Further, the instructions for this update in the Spree docs don’t mention it, so, as a reminder - after updating be sure to run
$ bundle exec rake railties:install:migrations
$ bundle exec rake db:migrate
Now, here is the tricky one. After updating and installing/running your migrations you may find that you are getting an error:
undefined method `assume_from_symbol' for Money:Class (ActionView::Template::Error)
I have been unable to find …
ecommerce ruby rails security spree update
RailsConf 2014: Highlights from Day One

I’m here in Chicago for RailsConf 2014, my fifth RailsConf! This time I’m attending the conference with my sidekick, and of course my sidekick’s babysitter, so I’m having an experience a bit different than previous attendance.
The Bees Knees
One of the first talks I attended today was Saving the World (literally) with Ruby on Rails by Sean Marcia.
Sean works with a Professor at George Mason University who researches bees and dying colonies. Armed with Raspberry Pis powered by solar panels, Sean uses the following technologies to monitor beehive metrics:
- gpio, pi_piper, wiringpi-ruby
- dashing
- Ruby & Sinatra. Sean had previously used Rails but found it a bit too heavyweight and went with a lighter Sinatra app.
- 3 Cronjobs to record data
- passenger, isc-dhcp-server, hostapd, iw for server configuration
Hive temperature, hive weight, outside temperature, and humidity are monitored in hopes of collecting metrics for identifying collapsing hives. The project is quite young, but the hope is to collect more metrics (e.g. gas permeability) and more actionable data as time goes on.
Views as Objects
Another interesting and relevant talk I attended today was …
conference ruby rails
Chrome, onmousemove, and MediaWiki JavaScript
tl;dr: avoid using onmousemove events with Google Chrome.
I recently fielded a complaint about not being able to select text with the mouse on a wiki running the MediaWiki software. After some troubleshooting and research, I narrowed the problem down to a bug in the Chrome browser regarding the onmousemove event. The solution in this case was to tweak JavaScript to use onmouseover instead of onmousemove.
The first step in troubleshooting is to duplicate the problem. In this case, the page worked fine for me in Firefox, so I tried using the same browser as the reporter: Chrome. Sure enough, I could no longer hold down the mouse button and select text on the page. Now that the browser was implicated, it was time to see what it was about this page that caused the problem.
It seemed fairly unlikely that something like this would go unfixed if it was happening on the flagship MediaWiki site, Wikipedia. Sure enough, that site worked fine, I could select the text with no problem. Testing some other random sites showed no problems either. Some googling indicated others had similar problems with Chrome, and gave a bunch of workarounds for selecting the …
browsers chrome mediawiki troubleshooting
Firefox, Input (type=button), and Line-Height
I recently discovered a discrepancy in the way Firefox treats inputs with a line-height style defined and how other browsers handle the same styling rule. Specifically, Firefox completely ignores it.
This behavior seemed odd enough to me that I did some Googling to determine if this was recently introduced, a long standing issue, or something I was just doing wrong. I found some interesting discussions on the issue. Several of the search results used the word “bug” in the title though it appears to be more of a deliberate (though possibly outdated) “feature” instead. Along with the discussions, I also came across a couple of suggestions for a solution.
First of all, I was able to locate a simple enough explanation of what’s causing the behavior. As Rob Glazebrook explains:
Basically, Firefox is setting the line-height to “normal” on buttons and is enforcing this decision with an !important declaration.” and, “browser-defined !important rules cannot be over-ruled by author-defined !important rules. This rule cannot be overruled by a CSS file, an inline style—anything.
Good news is I can stop experimenting hoping for different results.
I also located a Bugzilla ticket opened in …
browsers