[ rails ] retrieving filtered data from multiple tables

x = Person .select('people.id, people.name, comments.text') .joins(:comments) .where('comments.created_at > ?', 1.week.ago)

x will be a #Person object

The result should be something like this:

SELECT people.id, people.name, comments.text
FROM people
INNER JOIN comments
 ON comments.person_id = people.id
WHERE comments.created_at > '2015-01-01'

ref : http://guides.rubyonrails.org/active_record_querying.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s