user827570
user827570

Reputation: 491

How do you access inner relations in rails active association?

I have my associates set up in this manner.

event - (has_many) - vendor - (has_many) - product - (has_many) - productTags - (belongs_to) - tags

I'm trying to retrieve an array of tags for an event but only including those tags that have products.

Initially I have access to the event model through

@event = Event.find(1)

I'm having a really hard time going down the rabbit hole of associations to retrieve just the array of tags for all products.

Any help or a push in the right direction would be greatly appreciated.

Thanks a lot

Upvotes: 0

Views: 49

Answers (1)

Valery Kvon
Valery Kvon

Reputation: 4496

class Event < ActiveRecord::Base

  def tags
    Tag.includes(:product_tags => {:product => {:vendor => {:event => {}}}}).where(["events.id = ?", self.id])
  end

end

@event = Event.find(1)
@event.tags

Upvotes: 1

Related Questions