Skip to content

Instantly share code, notes, and snippets.

@imajes
Last active August 29, 2015 14:12
Show Gist options
  • Save imajes/a7409df21936c4d22cbf to your computer and use it in GitHub Desktop.
Save imajes/a7409df21936c4d22cbf to your computer and use it in GitHub Desktop.

Given these relationships:

User

  has_many :queue_items
  has_many :things, through: :queue_items


QueueItem

  belongs_to :user
  belongs_to :thing

Thing

  has_many :queue_items

The mechanism I have to use is as follows:

@user = User.find(n)

respond_with @user.things

... because the api should return the items, which are the real data objects we care about.

BUT,

there's a couple of attributes in the queue I care about. created_at, and widget, which are important to display.

so, Q: How do i access the queue object given this setup?

Obviously i can't do thing.queue_item --- because it doesn't exist.

And I know we've gotten the data, because we had to join on the queue table to get at it.

In pure sql, this will return all the data we care for:

SELECT queue_items.*, things.* FROM queue_items INNER JOIN things ON queue_items.thing_id = things.id WHERE queue_items.user_id = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment