From 66001f360661fefad89d9e271b9ff75a86b4b886 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sat, 13 Apr 2013 21:19:17 -0400 Subject: fix respond_to? for non selected column fixes #4208 If a query selects only a few columns and gives custom names to those columns then respond_to? was returning true for the non selected columns. However calling those non selected columns raises exception. post = Post.select("'title' as post_title").first In the above case when `post.body` is invoked then an exception is raised since `body` attribute is not selected. Howevere `respond_to?` did not behave correctly. pos.respond_to?(:body) #=> true Reason was that Active Record calls `super` to pass the call to Active Model and all the columns are defined on Active Model. Fix is to actually check if the data returned from the db contains the data for column in question. --- activerecord/CHANGELOG.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'activerecord/CHANGELOG.md') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 278da322f2..e46ca3bd07 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,28 @@ ## Rails 4.0.0 (unreleased) ## +* If a query selects only a few columns and gives custom names to + those columns then `respond_to?` was returning true for the non + selected columns. However calling those non selected columns + raises exception. + + post = Post.select("'title' as post_title").first + + In the above case when `post.body` is invoked then an exception is + raised since `body` attribute is not selected. Howevere `respond_to?` + did not behave correctly. + + pos.respond_to?(:body) #=> true + + Reason was that Active Record calls `super` to pass the call to + Active Model and all the columns are defined on Active Model. + + Fix is to actually check if the data returned from the db contains + the data for column in question. + + Fixes #4208. + + *Neeraj Singh* + * Run `rake migrate:down` & `rake migrate:up` in transaction if database supports. *Alexander Bondarev* -- cgit v1.2.3