ruby on rails - Access columns from two different tables -
i using rails 4.2. have model user
, comment
class user has_many :comments end class comment belongs_to :user end
user has column name
, comment has column user_id
, content
want fetch columns comment.content
, user.name
using join can this
comment.joins(:user).select(:name,:content)
can suggest efficient method perform action?
you can namespace columns following:
comment.joins(:user).pluck('users.name', 'comments.content') # though, don't need `comments.content`; `content` work well.
Comments
Post a Comment