Kevin Whitaker
Kevin Whitaker

Reputation: 13425

Rails: Retrieve all records that have the same variable value in a column

I've been asked to right a report of users that have the same billing address. Of course, I don't have a list of addresses to compare against, so is there a way to return all records that share an address with another record?

Thanks!

Upvotes: 0

Views: 1580

Answers (2)

user380692
user380692

Reputation: 246

A solution I can think of is doing it through SQL query.

Addresses.find_by_sql("SELECT * FROM addresses WHERE addresses.addr_line_1 IN (SELECT addr.addr_line_1 FROM addresses AS addr)......")

Hope this helps.

Upvotes: 0

Bohdan
Bohdan

Reputation: 8408

Hi how about something like

Model.find_by_address(Model.find_by_id("1").address)

if you need a list to compare

Model.all(:group => :address).map(&:address)

Upvotes: 1

Related Questions