user3437778
user3437778

Reputation: 37

rails-4 not getting all columns from 2 tables with join

This is my model.rb

model.rb

class Compute
belongs_to :user
end

class User
has_many :computes
end

I have used this query to get all details

User.joins(:computes).where.not(skey: 'NULL')

and i got all from USER table,also i need to get one or more column from COMPUTE with USER.

Upvotes: 3

Views: 748

Answers (2)

Manish
Manish

Reputation: 81

Instead of .select use .pluck. Like

Table_1.joins(:table_2).where(:conditions).pluck('table_1.col_1', 'table_1.col_2', 'table_2.col_1', 'table_2.col_2')

User.joins(:computes).where.not(skey: 'NULL').pluck('users.id', 'computes.name')

Upvotes: 1

amtest
amtest

Reputation: 700

You can use like this

User.joins(:computes).where.not(skey: 'NULL').select("users.id, computes.name")

Upvotes: 1

Related Questions