Tuesday, 15 September 2015

ruby on rails - ActiveRecord to fetch data from 2 tables by joining using find_by_sql -


I know there are a few ways to join / include tables, but here I want to get data from 2 tables By using ActiveRecord.find_by_sql.

example) have 2 tables.

  Table 1: User {id: integer name: string} Table 2: UserComment {id: Integer user_id: integer comment: string}  

What is the need to recover ActiveRecord

  select u.names, count (UK comment) user u, userm uc in U.ID = UK.USRIID group U.Name  

How can we write it with activation code? Return only to the U. N. name below: Select user.find_by_sql (& lt; & lt; -sql), count user (uc.comment) from user u, Usermment uc where u.id = UK.USUR_ID group sql by UK name

Is there a possible way of using the base.connection.Secute to the SEC? Umm, but it does not look the best .

  sql = 'Select UEAM, User U, User Comment from UED = UK.USURIID Group U.Name' Acti If you have a , then you have a  .base.cnation.exeqsq < Is_many: user_comments  defined in your  user  model: 

  User.joins (: user_comments) .group (: name) .count  

No comments:

Post a Comment