Getting a real int from DB


#1

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.


#2

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


#3

No problem, I’ll do with it :slight_smile:
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