User has_one UserProfile which has_one Community.
Given a community_id, I want a list of emails from the Users table.
How do I get a list of all User.email where community_id = 5?
The best I can find (which does work) is:
User.select(:email).joins(:user_profile).merge(UserProfile.for_community(5))
But it seems brute-force… is there not a way to do something simpler along the lines of this below?
User.user_profile.community(5)
I don’t think you know what your code does…
If the UserProfile has_one community, then the communities table must have a column titled user_id. That means there is only one User associated with any given community id.
To solve your question, as stated, you could delegate the user method to the user_profile.
Then you could do
Community.find(community_id).user.emailHope that helps.