rails/rails — Issue #3391: ActiveRecord does not type_cast attributes from joined tables
Author: ngauthier
Created: 2011-10-21 16:42:37+00:00
State: open

Not sure if bug or feature. Let's say I have:

```ruby
class Post < ActiveRecord::Base
has_many :comments
end

class Comment < ActiveRecord::Base
belongs_to :post
end
```

If I ask for a post's disabled state:

```ruby
Post.find(@post_id).disabled
=> true
```

I get a boolean.

But if I grab all the comments for the post, and I also join in the post's "disabled" boolean attribute:

```ruby
comments = Comment.where(:post_id => @post_id).joins(:posts).select('comments.*, posts.disabled')
comments.first.disabled
=> 't'
```

I believe that since `disabled` is not a column on the `comments` table the driver is not confident it can run `type_cast` on the column, giving "boolean" attributes of 't' and 'f' (under PG, but under other drivers it would be different).

I can manually run:

```ruby
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(comments.first.disabled)
```

But that's kind of lame.

xoxo @ngauthier