Forum: Ruby on Rails getting a real int from DB

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mickael F. (Guest)
on 2008-12-10 00:30
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.
Frederick C. (Guest)
on 2008-12-10 00:44
(Received via mailing list)
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
Mickael F. (Guest)
on 2008-12-10 09:25
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
This topic is locked and can not be replied to.