{"id":487,"date":"2017-10-25T11:58:00","date_gmt":"2017-10-25T09:58:00","guid":{"rendered":"http:\/\/www.labo.mathieurella.fr\/?p=487"},"modified":"2020-10-25T12:03:31","modified_gmt":"2020-10-25T11:03:31","slug":"create-dummy-data-using-faker-ruby-on-rails-tutorials","status":"publish","type":"post","link":"https:\/\/www.labo.mathieurella.fr\/?p=487","title":{"rendered":"Create Dummy Data Using Faker | Ruby on rails tutorials"},"content":{"rendered":"\n<p>There are many times, where customers want to see how their projects are working, how they are progressing and faker can be a solution to give them what they want, it is also essential for testing purposes.<\/p>\n\n\n\n<p>A few weeks ago, a client asked me to build a peer to peer platform, once everything was up and running, but something was missing, not least from the users.<\/p>\n\n\n\n<p>As a developer I couldn&#8217;t make sure I could find a myriad of real ones in a day. So instead of spending hours creating fake accounts or populating the database directly, I decided to create one.<\/p>\n\n\n\n<p>Faker appeared to me, as the solution, 20min, and 2 scripts later the site had its real fake users.<\/p>\n\n\n\n<p>Faker is a gem\u00a0that generates fake data.<\/p>\n\n\n\n<p>It comes in very handy for having real-looking test data, and having your database populated with more than one or two records while you&#8217;re doing development.<\/p>\n\n\n\n<p>Here is my script to populate my user, category and request table.<\/p>\n\n\n\n<p>Enjoy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10.times do\n    Category.create(\n        name: Faker::Job.unique.field\n    )\nend\n\n# Create dummy users\n5.times do\n    user = User.create(\n        full_name: Faker::Name.name,\n        email: Faker::Internet.free_email,\n        about: Faker::Quote.matz,\n        password: '123456',\n        from: Faker::Address.country,\n        language: Faker::Nation.language,\n        created_at: Date.today\n    )\n\n    user.avatar.attach(                \n        io: image = open(\"https:\/\/i.pravatar.cc\/300\"),\n        filename: \"avatar#{user.id}.jpg\", \n        content_type: 'image\/jpg'\n    )\n end\n\n# Create dummy Requests\n\n10.times do\n    random_user = User.all.sample(1)&#91;0]\n    category = Category.all.sample(1)&#91;0]\n    request = Request.create(\n        title: Faker::Job.title,\n        description: Faker::Quote.matz,\n        budget: Faker::Number.between(5, 50),\n        delivery: Faker::Number.between(1, 10),\n        user_id: random_user.id,\n        category_id: category.id\n    )\nend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are many times, where customers want to see how their projects are working, how they are progressing and faker &#8230;<\/p>\n","protected":false},"author":1,"featured_media":489,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/posts\/487"}],"collection":[{"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=487"}],"version-history":[{"count":2,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/posts\/487\/revisions\/490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=\/wp\/v2\/media\/489"}],"wp:attachment":[{"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.labo.mathieurella.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}