# Getting a real int from DB

**URL:** <https://www.ruby-forum.com/t/getting-a-real-int-from-db/155264>\
**Category:** Rails\
**Created:** [December 9, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/getting-a-real-int-from-db/155264 "2008-12-09T23:30:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 9, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/getting-a-real-int-from-db/155264/1 "2008-12-09T23:30:32Z")

</div>

Hi,

I have a User class with many projects :

has\_many :projects, :through=\>:membership, :select =\>  
“memberships.active, projects.\*”

The active field in the memberships table is an int.

So why this works :

def active\_projects  
projects.select{|p| p.active==‘1’}.sort\_by {|p| [p.name.upcase]}  
end

while this does not :

def active\_projects  
projects.select{|p| p.active==1}.sort\_by {|p| [p.name.upcase]}  
end

The difference is just that I compare with an integer instead of a  
string in the first case. I was expecting p.active to be a integer, not  
a string.

Thanks,  
Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 9, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/getting-a-real-int-from-db/155264/2 "2008-12-09T23:44:55Z")

</div>

On 9 Dec 2008, at 22:30, Mickael Faivre-Macon wrote:

> So why this works :
> 
> The difference is just that I compare with an integer instead of a  
> string in the first case. I was expecting p.active to be a integer,  
> not  
> a string.

When you pick attributes not from the base table (in this case  
projects) rails will handle things as strings. Maybe not what one  
would hope for, but it’s the way things are right now.

Fred

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 10, 2008, 8:25am UTC](https://www.ruby-forum.com/t/getting-a-real-int-from-db/155264/3 "2008-12-10T08:25:12Z")

</div>

No problem, I’ll do with it 🙂  
Thanks for this explanation.  
MIckael.

Frederick C. wrote:

> When you pick attributes not from the base table (in this case  
> projects) rails will handle things as strings. Maybe not what one  
> would hope for, but it’s the way things are right now.
> 
> Fred
