getting a real int from DB

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.

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.

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

No problem, I'll do with it :slight_smile:
Thanks for this explanation.
MIckael.

Frederick Cheung wrote: