ga-wdi-exercises / project3

[Project, Mongo, Mongoose, Node, Express, React, API] Prompt for 3rd WDI Project using React, Node, Mongo, Express and Mongoose
3 stars 4 forks source link

Seeding JSON Data into DB #137

Closed christine-sm closed 8 years ago

christine-sm commented 8 years ago

We have 3 tables: zoos, mammals, and reptiles. Mammals and reptiles belong to zoos, and we are having difficulty seeding mammals and reptiles into the DB so that the data belongs to zoos. seeds.rb looks like this:

zoos_data = JSON.parse(File.read("db/zoos_data.json"))
mammals_data = JSON.parse(File.read("db/mammals_data.json"))
reptiles_data = JSON.parse(File.read("db/reptiles_data.json"))
Zoo.destroy_all
Mammal.destroy_all
Reptile.destroy_all
zoo = Zoo.create!(zoos_data)
mammals_seed = zoo.mammals.create!(mammals_data)
reptiles_seed = zoo.reptiles.create!(reptiles_data)

I also tried to put mammals_seed and reptiles_seed into a do loop but that didn't work. We keep receiving the following error NoMethodError: undefined methodmammalsfor #<Array:0x007fea4cac90e8>. The zoos data is successfully being seeded in. I have gone into rails console and it understands the zoo.find(1).mammals relationship there. Any suggestions?

RobertAKARobin commented 8 years ago

Can you show me an example of an entry in your mammals_data.json file?

christine-sm commented 8 years ago

It's long so get ready. We ran all of our data through a JSON validator and it is valid. This is our first mammal (I added the ] at the end for this snippet):

[{
  "common_name": "Giant Panda",
  "scientific_name": "Ailuropoda melanoleuca",
  "zoo_location": "Giant Panda Habitat",
  "phys_descrip": "The giant panda, a black-and-white bear, has a body typical of bears. It has black fur on ears, eye patches, muzzle, legs, and shoulders. The rest of the animal's coat is white. Although scientists do not know why these unusual bears are black and white, some speculate that the bold coloring provides effective camouflage into their shade-dappled snowy and rocky surroundings. The panda's thick, wooly coat keeps it warm in the cool forests of its habitat. Giant pandas have large molar teeth and strong jaw muscles for crushing tough bamboo. Many people find these chunky, lumbering animals to be cute, but giant pandas can be as dangerous as any other bear.",
  "size": "About the size of an American black bear, giant pandas stand between two and three feet tall at the shoulder (on all four legs), and reach four to six feet long. Males are larger than females, weighing up to 250 pounds in the wild. Females rarely reach 220 pounds.",
  "geo_distribution": "Giant pandas live in a few mountain ranges in central China, in Sichuan, Shaanxi, and Gansu provinces. They once lived in lowland areas, but farming, forest clearing, and other development now restrict giant pandas to the mountains. Giant pandas live in broadleaf and coniferous forests with a dense understory of bamboo, at elevations between 5,000 and 10,000 feet. Torrential rains or dense mist throughout the year characterizes these forests, often shrouded in heavy clouds.",
  "status": "The giant panda is listed as endangered in the World Conservation Union's (IUCN's) Red List of Threatened Species. There are about 1,600 left in the wild. More than 300 pandas live in zoos and breeding centers around the world, mostly in China.",
  "natural_diet": "A wild giant panda’s diet is almost exclusively (99 percent) bamboo. The balance consists of other grasses and occasional small rodents or musk deer fawns.",
  "zoo_diet": "In zoos, giant pandas eat bamboo, sugar cane, rice gruel, a special high-fiber biscuit, carrots, apples, and sweet potatoes.",
  "reproduction": "Giant pandas reach breeding maturity between four and eight years of age. They may be reproductive until about age 20. Female pandas ovulate only once a year, in the spring. A short period of two to three days around ovulation is the only time she is able to conceive. Calls and scents draw males and females to each other.\nFemale giant pandas give birth between 95 and 160 days after mating. Although females may give birth to two young, usually only one survives. Giant panda cubs may stay with their mothers for up to three years before striking out on their own. This means a wild female, at best, can produce young only every other year; in her lifetime, she may successfully raise only five to eight cubs. The giant pandas’ naturally slow breeding rate prevents a population from recovering quickly from illegal hunting, habitat loss, and other human-related causes of mortality.",
  "life_span": "Scientists aren't sure how long giant pandas live in the wild, but they are sure it's shorter than lifespans in zoos. Chinese scientists have reported zoo pandas as old as 35. The National Zoo's Hsing-Hsing died at age 28 in 1999.",
  "behavior": "Until recently, scientists thought giant pandas spent most of their lives alone, with males and females meeting only during the breeding season. Recent studies paint a different picture, in which small groups of pandas share a large territory and sometimes meet outside the breeding season. Much remains to be learned about the secret lives of these elusive animals, and every new discovery helps scientists in their battle to save this species. Offspring stay with their mothers from one and a half to three years.",
  "fun_facts": "A giant panda’s digestive system is more similar to that of a carnivore than an herbivore, and so much of what is eaten is passed as waste. To make up for the inefficient digestion, a panda needs to consume a comparatively large amount of food—from 20 to 40 pounds of bamboo each day—to get all its nutrients. To obtain this much food means that a panda must spend 10 to 16 hours a day foraging and eating. The rest of its time is spent mostly sleeping and resting.",
  "image_one": "https://usatftw.files.wordpress.com/2015/12/ap-national-zoo-panda_002.jpg?w=1000&h=600&crop=1",
  "image_two": "http://ids.si.edu/ids/deliveryService?id=http://sirismm.si.edu/SPI/5562_Image_Panda_201.jpg&max_w=200",
  "video": "https://youtu.be/X-Isg_pJy2c"
}]
RobertAKARobin commented 8 years ago

OK. Can you show me the entry for the panda's zoo in zoos_data.json? I want to get a better sense of what your data looks like.

christine-sm commented 8 years ago
[{
  "name":"Smithsonian National Zoological Park",
  "hours_of_op":"Visitor Center & Exhibit Buildings: 9 a.m. to 6 p.m. (4 p.m. closing in winter*; 10 a.m. opening for Amazonia and the Bird House year-round)\n Grounds: 8 a.m. to 7 p.m. (5 p.m. closing in winter*)\n Concessions 9 a.m. to 5 p.m. (4:30 p.m. closing in winter*)\n Rock Creek bike path 6 a.m. to 7 p.m.\n *Winter hours: October 1-March 14.",
  "location": "The Smithsonian's National Zoo is located in the heart of Northwest Washington, DC and spans 163 acres. There are several entrances to the Zoo and all are pedestrian and vehicle-friendly. The address of the zoo is as follows: \n 3001 Connecticut Ave NW\n Washington, DC 20008",
  "transpo_info": "The Zoo recommends that visitors use the Washington Metropolitan Area Transit Authority (WMATA) Trip Planner to navigate to the Zoo. The Zoo is accessible via Metro Bus and Metro Rail. Both the Woodley Park and Cleveland Park Metro stops are within walking distance of the Zoo.\n Parking is available at the Zoo for a flat fee of $22 or FREE with some FONZ membership levels. Reserved parking at or near the Zoo can be purchased ahead of time through Parking Panda.\n Capital Bikeshare has several stations for rental bikes near the Zoo. The Zoo recommends that visitors check the Capital Bikeshare website for locations and availability of rentals."
}]
christine-sm commented 8 years ago

We just have one zoo

RobertAKARobin commented 8 years ago

Since you have just the one zoo, you could do something like:

zoo = Zoo.create!(zoo_data[0])
zoo.mammals.create!(mammals_data)
christine-sm commented 8 years ago

That works!! Thank you.

RobertAKARobin commented 8 years ago

No problem! If you have multiple you could do something like this:

Zoo data:

[
  {"name": "Smithsonian Zoo"},
  {"name": "San Diego Zoo"}
]

Mammal data:

[
  {"zoo_name": "Smithsonian Zoo", "animal_data": {"common_name": "Panda", "size": "Big"}},
  {"zoo_name": "San Diego Zoo", "animal_data": {"common_name": "Grizzly Bear", "size": "Also big"}}
]
Zoo.create!(zoo_data)
mammal_data.each do |mammal|
  zoo = Zoo.find_by(name: mammal[:zoo_name])
  zoo.mammals.create!(mammal[:animal_data])
end