Rails 3 Joins -- Select Only Certain Columns

Rails 3 Joins -- Select only certain columns

You could use something like this:

@comments = Comment.joins(:user)
.select("comments.*, users.first_name")
.where(study_id: @study.id)

Ruby rails - select only few columns from the data base

Rails 3:

Item.select("name, address").where( .... )

Rails query - How to joins two tables on specific columns

Breaking the parts out for clarity.

The tables involved...

account_table = Account.arel_table
transfership_table = OwnershipTransfer.arel_table

The arel join...

join = account_table.join(transfership_table).on(account_table[id].eq( transfership_table[:transfer_from_id] ))

Using the arel join in the query...

Account.joins(join.join_sources).where(ownership_transfers: {meeting_event_id: 458})

in Rails how to select additional columns from multiple tables

result = User.first.usertags.joins(:tag).select("tags.name as tag_name, tags.id, usertags.id, user.id")

result.collect{|r| r.tag_name}


Related Topics



Leave a reply



Submit