Skip to content

Commit

Permalink
define_attr_method must serialize nil correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Dec 21, 2010
1 parent 099a210 commit 207f266
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -109,7 +109,7 @@ def define_attr_method(name, value=nil, &block)
# use eval instead of a block to work around a memory leak in dev
# mode in fcgi
sing.class_eval <<-eorb, __FILE__, __LINE__ + 1
def #{name}; #{value.to_s.inspect}; end
def #{name}; #{value.nil? ? 'nil' : value.to_s.inspect}; end
eorb
end
end
Expand Down
Expand Up @@ -24,15 +24,19 @@ def reset_primary_key #:nodoc:
end

def get_primary_key(base_name) #:nodoc:
return unless base_name
return unless base_name && !base_name.blank?

case primary_key_prefix_type
when :table_name
base_name.foreign_key(false)
when :table_name_with_underscore
base_name.foreign_key
else
'id'
if ActiveRecord::Base != self && connection.table_exists?(table_name)
connection.primary_key(table_name)
else
'id'
end
end
end

Expand Down
Expand Up @@ -9,6 +9,11 @@ def setup
:timeout => 100
end

def test_primary_key_returns_nil_for_no_pk
@conn.exec_query('create table ex(id int, data string)')
assert_nil @conn.primary_key('ex')
end

def test_connection_no_db
assert_raises(ArgumentError) do
Base.sqlite3_connection {}
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -512,6 +512,10 @@ def test_create_associate_when_adding_to_has_many_through
assert_nothing_raised { vertices(:vertex_1).sinks << vertices(:vertex_5) }
end

def test_add_to_join_table_with_no_id
assert_nothing_raised { vertices(:vertex_1).sinks << vertices(:vertex_5) }
end

def test_has_many_through_collection_size_doesnt_load_target_if_not_loaded
author = authors(:david)
assert_equal 10, author.comments.size
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -19,6 +19,7 @@
require 'models/warehouse_thing'
require 'models/parrot'
require 'models/loose_person'
require 'models/edge'
require 'rexml/document'
require 'active_support/core_ext/exception'

Expand Down Expand Up @@ -48,6 +49,10 @@ class Boolean < ActiveRecord::Base; end
class BasicsTest < ActiveRecord::TestCase
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts

def test_primary_key_with_no_id
assert_nil Edge.primary_key
end

def test_select_symbol
topic_ids = Topic.select(:id).map(&:id).sort
assert_equal Topic.find(:all).map(&:id).sort, topic_ids
Expand Down

0 comments on commit 207f266

Please sign in to comment.