April 18th, 2010
If you are getting this error during a sphinx index, it is likely because you have a copy of the gem ‘thinking-sphinx’ installed, but you also have an older version of the gem from the old github repository also installed (eg, freelancing-god-thinking-sphinx).
The solution is to remove the old github gem version.
Also this should be a reminder that github will stop hosting gems in October 2010, so you need to allocate time to switching all of your gem dependencies be running gems from github to their respective rubygems/gemcutter versions.
Categories: plugins
Tags: errors, thinking sphinx
April 9th, 2010
I write code for a living. Even when I am writing code by myself, there are actually three people working with that code.
First, there is me. I am a clever and talented programmer. I know to keep my code clean and easy to read. I try to do things the right way and I know that if old code is wrong that I am better off fixing it now then trying to code around it.
Second, there is past me. This guy is a fracking moron. He pisses me off on a daily basis as I am having to constant re-write his code. He is generally clueless and causes me more headaches than anyone else.
Finally there is the future me. This guy is smarter than I am, has seen more than I have and has done more. My goal, in addition to making sure my code works, is to make sure that my intentions are clear for him so that I don’t piss him off too badly.
When I claim to be a moron, it is a matter of perspective. Compared to my 8 year old self, I am brilliant. Compared to myself in 10 years; not so much. Therefore, it is safe to say that anything I wrote in the past was written by someone dumber than I am.
—-
Saw that here: http://www.cimgf.com/2009/03/26/dont-blindly-trust-debb/ – very eloquently expressed, but really focuses in on the fact that as programmers we are always improving.
Categories: Productivity
Tags: interesting ideas
March 30th, 2010
Migration:
create_table :addresses do |t|
....
t.references :addressable, :polymorphic => true
t.string :type
t.timestamps
end
Address Models:
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
class InvoiceAddress < Address
end
class DeliveryAddress < Address
end
User Model:
class User < ActiveRecord::Base
has_many :addresses, :dependent => :destroy
has_one :invoice_address, :as => :addressable
has_one :delivery_address, :as => :addressable
end
Order Model:
class Order < ActiveRecord::Base
has_many :addresses, :dependent => :destroy
has_one :invoice_address, :as => :addressable
has_one :delivery_address, :as => :addressable
end
Get Funky:
>> u = User.new
=> #<User id: nil ...>
>> u.build_invoice_address
=> #<InvoiceAddress id: nil, addressable_id: nil, addressable_type: "User", created_at: nil, updated_at: nil, type: "InvoiceAddress">
>> u.build_delivery_address
=> #<DeliveryAddress id: nil, addressable_id: nil, addressable_type: "User", created_at: nil, updated_at: nil, type: "DeliveryAddress">
>> o = Order.new
=> #<Order id: nil ...>
>> o.build_invoice_address
=> #<InvoiceAddress id: nil, addressable_id: nil, addressable_type: "Order", created_at: nil, updated_at: nil, type: "InvoiceAddress">
Sweet
Categories: activerecord
Tags: activerecord
February 10th, 2010
Sometimes you have a Rails app, the core functionality is not a CMS, and you need to add some static pages to your app that don’t get changed often, eg, an about us page, privacy policy, etc.
This post shows you a quick and easy way to do that in about 60 seconds, without using a database model or having to write any sort of CMS.
1) First off, add some routes for your static pages. Here I want an FAQ page, About page, Privacy page, and Terms and Conditions pages.
map.with_options :controller => 'static_pages', :action => 'show' do |static_page|
static_page.faq 'faq', :id => 'faq'
static_page.about 'about', :id => 'about'
static_page.privacy 'privacy', :id => 'privacy'
static_page.terms_and_conditions 'terms_and_conditions', :id => 'terms_and_conditions'
end
2) Add a functional test so we know it works now, and we can check we don’t retrospectively break it later. Create a new file in test/functionals/static_pages_controller_test.rb:
require File.dirname(__FILE__) + '/../test_helper'
class StaticPagesControllerTest < ActionController::TestCase
should_route :get, '/privacy', :action => 'show', :id => 'privacy'
should_route :get, '/terms_and_conditions', :action => 'show', :id => 'terms_and_conditions'
should_route :get, '/faq', :action => 'show', :id => 'faq'
should_route :get, '/about', :action => 'show', :id => 'about'
%w(privacy about terms_and_conditions faq).each do |static_page_template|
context "Accessing the #{static_page_template} page" do
setup do
get :show, :id => static_page_template
end
should_respond_with :success
should_render_template static_page_template
end
end
end
This is fairly simple but covers everything we need it to. It just checks your routing is setup and the pages render from the static template files correctly.
3) Create your controller, app/controllers/static_pages_controller.rb:
class StaticPagesController < ApplicationController
def show
page = params[:id]
render :template => "static_pages/#{params[:id]}"
end
end
Again, really simple. Use a RESTful ’show’ method to select a template to render, then render it.
4) from a shell, make a directory for your pages to live in, and create the empty files:
$ mkdir app/views/static_pages
$ touch app/views/static_pages/about.html.erb
$ touch app/views/static_pages/faq.html.erb
$ touch app/views/static_pages/privacy.html.erb
$ touch app/views/static_pages/terms_and_conditions.html.erb
5) Check it works:
$ ruby test/functional/static_pages_controller_test.rb
Loaded suite test/functional/static_pages_controller_test
Started
............
Finished in 0.156958 seconds.
12 tests, 24 assertions, 0 failures, 0 errors
Done! Now put the content in your pages, test, check in and deploy.
Categories: Rails
Tags: development, Rails, Testing
February 7th, 2010
In case you haven’t heard, Rails 3 is now in Beta.
Just a quick post to say, if you haven’t already, check out the Rails upgrade plugin, this will quickly show you what bits of your code need to be updated to work with Rails 3.
Also, check out the full release notes, lots of juicy stuff in there.
Categories: Rails
Tags: Rails 3