Create a gist now

Instantly share code, notes, and snippets.

Rails 3.1: HasManyThrough Bug
require "sqlite3"
#gem "rails", "~> 3.0.10"
#gem "rails", "3.1.0"
gem "rails", "3.1.1.rc1"
require "logger"
require "active_record"
require "test/unit"
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Schema.define do
create_table "people", :force => true do |t|
t.string "project_id"
t.string "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", :force => true do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :users, :through => :people
end
class Person < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
class User < ActiveRecord::Base
has_many :people
has_many :projects, :through => :people
end
class HasManyThrough < Test::Unit::TestCase
def test_user_projects_build_assigns_project_to_user
user = User.create(:name => "Francesc")
project = user.projects.build
assert !project.user.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment