Rails 4 scopes with has_many :through relations

Given the following Rails 4 model:

class Producer < ActiveRecord::Base has_many :producer_types has_many :types, :through => :producer_types
end

There are a couple ways you could create a scope on this model to retrieve only producers of a certain type.

First:

  scope :manufacturer, -> { where(:types => {:name => 'manufacturer'}).joins(:types) }

Second, to generate a dynamic scope for the given column name, you can take advantage of the following active record functionality:

  scope :with_type_name, lambda {|type_name|
    where(:types => {:name => type_name}).joins(:types)
  }

Great way to deal with trolls on your blog

Reading 37signals’ Signal vs. Noise blog today, I came across what I think is a great example of how to deal with trolls on your blog (would also work on a forum/bulletin board).

image of a blog comment with troll label and dunce hat

The problem with one of the most common responses to trolls (i.e. banning them), is that they just come right back and return to grabbing people’s attention with inflammatory messages and starting flame wars. This is a much more elegant solution: rather than getting rid of the offending user, clearly identify them so others won’t feed the troll, shame them with the dunce cap, and make their message slightly harder to read.

Apparently phpBB, a common web forum package in PHP, comes with some similarly creative ways of dealing with trolls, which include silencing them so that it looks to the troll like all their posts are going through, but no one is reacting. Apparently in the above case, the offending user was being impersonated, so I’ve blurred the username (not that it really matters much), but I don’t imagine a troll would stick around very long under such circumstances.

And as an aside, the SvN blog post itself was pretty interesting, discussing how 37signals had taken advantage of an interesting RoR “performance management solution” called New Relic to optimize their server configuration and cut response times drastically.

If only there were something similar for some of the PHP frameworks (maybe there is?).