Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:23:29+00:00 2026-06-07T13:23:29+00:00

I have users. Users can poke other users, as well as poke themselves. Each

  • 0

I have users. Users can poke other users, as well as poke themselves. Each poke is directional, and group pokes don’t exist. I want to list all pokes (incoming or outgoing) for a given user, without duplicating self-pokes (which exist as both incoming_ and outgoing_pokes).

Here are my models:

class User < ActiveRecord::Base
  has_many :outgoing_pokes, :class_name => "Poke", :foreign_key => :poker_id
  has_many :incoming_pokes, :class_name => "Poke", :foreign_key => :pokee_id
end

class Poke < ActiveRecord::Base
  belongs_to :poker, :class_name => "User"
  belongs_to :pokee, :class_name => "User"
end

I tried creating a method in the User model to merge the pokes:

def all_pokes
  outgoing_pokes.merge(incoming_pokes)
end

but that returns only the self-pokes (those that are both incoming_ and outgoing_pokes). Ideas? Is there a clean way to do this using associations directly?

Also, in the merged list, it’d be great to have two booleans for each poke to record how they’re related to the current user. Something like outgoing and incoming.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:23:31+00:00Added an answer on June 7, 2026 at 1:23 pm

    The reason your all_pokes method is returning only self-pokes is because outgoing_pokes is not an array yet, but an AR relationship that you can chain on. merge, in this case, combines the queries before executing them.

    What you want is to actually perform the queries and merge the result sets:

    def all_pokes
      (outgoing_pokes.all + incoming_pokes.all).uniq
    end
    

    …or you could write your own query:

    def all_pokes
      Poke.where('poker_id = ? OR pokee_id = ?', id, id)
    end
    

    Determining whether it’s incoming or outgoing:

    # in poke.rb
    def relation_to_user(user)
      if poker_id == user.id
        poker_id == pokee_id ? :self : :poker
      elsif pokee_id == user.id
        :pokee
      else
        :none
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database where users can list books they've read, want to
I want to create a website with various users. The users can have different
I have an application where users can store items. Now each type of account
i have users who can logon to my site. I don't use the asp.net
I'm working on a Rails application where I have users that can add each
I have website where users can login with their Facebook account. I am using
Im creating forum like application and I have textbox where users can enter tags
I have a website where users can purchase tickets, but ticket quantities are usually
I have several sites where users can send me email through an html form
I have an app where users can change a toggle value between true and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.