Created
December 14, 2013 02:23
-
-
Save octosteve/7954970 to your computer and use it in GitHub Desktop.
Joe Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
ohhh... first_or_create is a built in method. (noted.)
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
Ok, cool. Couple questions: