Skip to content

Instantly share code, notes, and snippets.

@octosteve
Created December 14, 2013 02:23
Show Gist options
  • Select an option

  • Save octosteve/7954970 to your computer and use it in GitHub Desktop.

Select an option

Save octosteve/7954970 to your computer and use it in GitHub Desktop.
Joe Example
#Say we have Category and Shirt models. We have a tags join model the bind the 2 together.
# Models look like this
class Category < ActiveRecord::Base
has_many :tags
has_many :shirts, through: :tags
end
class Shirt < ActiveRecord::Base
has_many :tags
has_many :categories, through: :tags
end
class Tag < ActiveRecord::Base
belongs_to :shirt
belongs_to :category
end
# OK. So we want to allow users to add multiple categories to a shirt through tags. The way this is set up, it's possible
# one shirt may have the same category as another. ie
vader_breakfast = Shirt.new
vader_breakfast.categories # some magic to add the category of Star Wars here
casual_friday = Shirt.new
casual_friday.categories # Same magic here
# what's that magic look like? Well, remember that this works
star_wars = Category.create name: "Star Wars"
vader_breakfast.categories << star_wars
# lovely! The problem appears when when we want to add another shirt with the category of star wars.
# Do we create the category again?! How do we deal with a system that sometimes has records and doesn't other times?
# Rails to the rescue
star_wars = Category.where(name: "Star Wars").first_or_create
casual_friday.categories << star_wars
# in this example, they both use the same star wars db record. http://apidock.com/rails/ActiveRecord/Relation/first_or_create
# you're dealing with a complex object but it's the same thing really, except you can pass an argument to create (Checkout the docs above)
events = facebook { |fb| fb.graph_call(event_graph_call)}
events["events"]["data"].each do |event|
event = Event.where(fb_id: event["id"]).first_or_create(
#notice I removed fb_id...
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"],
cover_id: event["cover_id"],
admin_id: event["admin_id"],
attendee_id: event["attendee_id"],
updated_time: event["updated_time"],
venue_id: event["venue_id"],
created_at: event["created_at"],
updated_at: event["updated_at"],
owner_id: event["owner"]["id"],
user_id: User.last["id"])
end
end
@josephdburdick
Copy link

Ok, cool. Couple questions:

  1. This isn't all in one file called find_or_create.rb, right? The class declarations at the top of the Gist would appear in their associated model.rb file, am I right? Consolidating all the code in one Gist was just so I could see and understand it better..correct me if I'm wrong.
  2. On line 38 you reference a method called first_or_create but this method isn't created any where in this file. The name of this Gist is find_or_create, so is this a typo and should be referencing this file itself?
  3. I was told by one of the TAs that it's important to record Facebook's ID for everything just in case I want to post to the wall of an event or send a message to a specific user (I would need the ID for that FB object to do so). When you begin creating the event variable on line 47 I see you check to see if the fb_id is the same as the event["id"]... could this check be the method first_or_create you meant to include? How do you check if fb_id matches the event["id"] if the fb_id hasn't stored anything yet?

@josephdburdick
Copy link

ohhh... first_or_create is a built in method. (noted.)

@octosteve
Copy link
Author

Looks like you have 1 and 2 down.

I don't understand question 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment