ActiveRecord joins: integer columns converted to s

2020-07-18 03:27发布

问题:

I have a model named Article, which I am joining with TwitterShare as shown below:

articles = Article.joins("LEFT OUTER JOIN twitter_shares ON articles.id = twitter_shares.article_id").where("articles.id = ? or articles.id = ?", 27165, 5632).select("articles.id, twitter_shares.user_id")

When I get the articles back, and examine the Article model returned, it returns the user_id as a string, even though the column type in the twitter_shares table is an integer. Why is this? How can I make ActiveRecord return the integer column as an integer, not a string?

回答1:

Why not just convert the returned user_id with .to_i where you need it as integer?