Question

I'm using Facebook Koala gem to pull in data pertaining to Events.

Please tell me why despite adding the correct event_id to venues, admins, attendees that I cannot then go @event.admins in the event view?

Code in User.rb, after omniauth logs me in.

events["events"]["data"].each do |event|
  if event["admins"]
    event_admins = event["admins"]["data"]
    if (is_admin_for_event? event_admins)

      @new_event = Event.where(fb_id: event["id"]).first_or_create(
        name:           event["name"],
        description:    event["description"],
        location_id:    event["location"],
        start_time:     event["start_time"],
        end_time:       event["end_time"],
        ticket_uri:     event["ticket_uri"],
        privacy:        event["privacy"],
        admin_id:       event["admin_id"],
        updated_time:   event["updated_time"],
        created_at:     event["created_at"],
        updated_at:     event["updated_at"],
        user_id:        self.id)

      @new_owner = Owner.where(fb_id: event["owner"]["id"]).first_or_create(
        name:         event["owner"]["name"])
      @new_owner.event_id = @new_event.id
      @new_owner.save!

      event["admins"]["data"].each do |admin|
        @new_admin = Admin.find_or_initialize_by(fb_id: admin["id"])

        if !@new_admin.events.include?(@new_event.id)
          @new_admin.events << @new_event
        end
        @new_event.save!
      end

      @new_venue = Venue.where(fb_id: event["venue"]["id"]).first_or_create(

        fb_id:        event["venue"]["id"],
        latitude:     event["venue"]["latitude"],
        longitude:    event["venue"]["longitude"],
        city:         event["venue"]["city"],
        state:        event["venue"]["state"],
        country:      event["venue"]["country"],
        street:       event["venue"]["street"],
        zip:          event["venue"]["zip"])

      if !@new_venue.events.include?(@new_event)
        @new_venue.event_id = @new_event.id
        @new_event.venues
        @new_venue.save!
      end  

      event["attending"]["data"].each do |attendee|
        new_attendee = Attendee.where(fb_id: attendee["id"]).first_or_create(
          name:        attendee["name"],
          first_name:  attendee["first_name"],
          last_name:   attendee["last_name"],
          email:       attendee["email"],
          rsvp_status: attendee["rsvp_status"],
          picture_url: attendee["picture"]["data"]["url"],
          event_id:    @new_event.id)    
      end

      if event.has_key?("maybe")
        event["maybe"]["data"].each do |attendee|
          new_attendee = Attendee.where(fb_id: attendee["id"]).first_or_create(
            name:       attendee["name"],
            first_name: attendee["first_name"],
            last_name:  attendee["last_name"],
            email:      attendee["email"],
            rsvp_status:attendee["rsvp_status"],
            picture_url:attendee["picture"]["data"]["url"],
            event_id:   Event.last["id"])
        end
      end
    end #/ is_admin_for_event?
  end #/event[admins]

While using pry in numerous places I get "nil" when trying @new_event.admins but when I try to say @new_event.admins << Admin.first it doesn't understand << as a method. Whole repository found here: https://github.com/josephdburdick/anycrawl

Thanks.

Was it helpful?

Solution

Associations were setup fine, I needed to create an admins method in my Event class.

Added this to my Event class:

def admins
    Person.joins(:attendees).where(attendees: {is_admin: true, event_id: self.id})
end

Works fine now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top