How do I get my torrents to work?


Aelo-74

I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed

Here is my seed:

require 'faker'

puts "Creating 10 fake user heros..."
10.times do
  user = User.create!(
    username: Faker::DcComics.hero,
    password: "123456",
    email: "#{user.username}@gmail.com",
    bio: "Hi, I'm #{user.username}. Faker::Movie.quote",
    avatar: Faker::LoremFlickr.image(size: "900x900", search_terms: ['dccomics'])
  )
  puts "#{user.username} created!"
end

puts "Creating 10 fake hero services..."
10.times do
  service = Service.create!(
    name: "#{Faker::Verb.ing_form} service",
    description: "I'll use my #{Faker::Superhero.power} to accomplish the mission",
    address: "#{Faker::Address.street_address}, #{Faker::Address.city}",
    photos: Faker::LoremFlickr.image(size: "900x900", search_terms: ["#{Faker::Verb.ing_form}"]),
    user_id: rand(user.id)
  )
  puts "#{service.name} created!"
end

puts "Finished!"

I think I'll run into a few problems in particular:

  • My interpolation method: I'm not sure if I can use it in Faker
  • I want to use the same username in the given usernameand email, what am I doing?
  • My User model doesn't have an incarnation in the database table, however has_one_attached :avatar, can I give it a seed?
  • Related question about my service model ( has_many_attached :photos) . How can I generate multiple images for it?
  • I want to randomly select a service model for me from user_idthe services created above , I really don't know how to proceed...
Heavy

My interpolation method: I'm not sure if I can use it in Faker

Interpolation works with any expression. even multiple statements. "#{ a = 1 + 1; b = 2 + 2; a + b}"will produce "6". But don't do that, it's confusing.

name: "#{Faker::Verb.ing_form} service",

equivalent to

name: Faker::Verb.ing_form.to_s + " service"

In some places, you are missing interpolation.

bio = "Hi, I'm #{user.username}. Faker::Movie.quote"

That should be...

bio = "Hi, I'm #{user.username}. #{Faker::Movie.quote}"

Elsewhere, interpolation is not required.

search_terms: ["#{Faker::Verb.ing_form}"]

Faker::Verb.ing_formA string is already returned, no interpolation is required.

search_terms: [Faker::Verb.ing_form]

I want to use the same username as provided in the email, am I doing it right?

No, you must usercreate it before you can reference it . If you try, you will get something similar undefined method `username' for nil:NilClass. userYes nil, there is nilalso no method called username.

Instead, you can create a user, set their email, and save.

user = User.new(
  username: Faker::DcComics.hero,
  password: "123456",
  avatar: Faker::LoremFlickr.image(size: "900x900", search_terms: ['dccomics'])
)
user.email = "#{user.username}@gmail.com"
user.bio = "Hi, I'm #{user.username}. #{Faker::Movie.quote}"
user.save!

Alternatively, you can create the username first and store it in a variable for reference.

  username = Faker::DcComics.hero
  user = User.create!(
    username: username,
    password: "123456",
    email: "#{username}@gmail.com",
    bio: "Hi, I'm #{username}. #{Faker::Movie.quote}",
    avatar: Faker::LoremFlickr.image(size: "900x900", search_terms: ['dccomics'])
  )

My user model has no avatar in the database table, but has_one_attached: avatar, can I seed it?

I'm not very familiar with attachments in Rails , but I think it should work as you wrote.


I want to feed my service model from one of the random user_id created above, I don't really know how to proceed...

Simple thing is to use .sample

user = User.all.sample

We can make it a little better by pluckjust getting the ID .

user = User.pluck(:id).sample

However, this will load all users. For small test databases this is not a problem. For production code, this is a huge waste. Instead, we can use order by random() limit 1. In Rails 6, it's a bit clunky.

User.order(Arel.sql('RANDOM()')).pluck(:id).first

See this answer for an explanation.

Note that if you already have users loaded, pass the user instead of its id. This avoids the service having to load the user again.

10.times do
  user = User.order(Arel.sql('RANDOM()')).pluck(:id).first
  service = Service.create!(
    ...
    user: user
  )
end

But there is a better way...


Related question about my service model (has_many_attached:photos). How can I generate multiple images for it?

Here we make a real leap. Instead of generating static test data, use a Factory. FactoryBot makes it easy to define "factories" using Faker to generate test data. You are halfway there. With FactoryBotRails , FactoryBot learns how to use Rails models.

The user factory looks like this.

FactoryBot.define do
  factory :user do
    username { Faker::DcComics.hero }
    password { "123456" }
    email { "#{username}@gmail.com" }
    bio { "Hi, I'm #{username}. #{Faker::Movie.quote}" }
    avatar { Faker::LoremFlickr.image(size: "900x900", search_terms: ['dccomics']) }
  end
end

Note that we can do it on a emailbasis username. Properties can reference other properties. This is because properties are actually little functions like this:

class UserFactory do
  def username
    @username ||= Faker::DDcComics.hero
  end

  def email
    @email ||= "#{username}@gmail.com"
  end
end

and make 10 of them...

users = FactoryBot.create_list(:user, 10)

You can override the default value. If you want users to use a certain email address...

user = FactoryBot.create(:user, email: "[email protected]")

Since you can keep users on the go, you no longer need to seed specific data.

Serving now. Let's take a look at the service factory. To provide the service, we need photos.

  factory :service do
    name { "#{Faker::Verb.ing_form} service" }
    description { "I'll use my #{Faker::Superhero.power} to accomplish the mission" }
    address { "#{Faker::Address.street_address}, #{Faker::Address.city}" }
    user
    # photos is presumably has-many_attached and so takes an Array.
    photos {
      [
       Faker::LoremFlickr.image(
         size: "900x900",
         search_terms: [Faker::Verb.ing_form]
       )
      ]
    }
  end

Take a look user. FactoryBot will automatically populate with users manufactured using your user factory. Fixed an issue with serving with random users.

Our final step is to dry our plant by defining a photo .

factory :photo, class: Faker::LoremFlickr do
  size { "900x900" }
  search_terms { Faker::Lorem.words }

  initialize_with do
    Faker::LoremFlickr.image(attributes)
  end
end

This is not a Rails model, so we need to teach FactoryBot how to make a photo by giving it its class and how to initialize it . attributesis a hash containing size and search_terms.

Also note that I used more general search terms. Factories that require specific search terms will provide their own factories.

We can make any number of photos.

photos = FactoryBot.build_list(:photos, 3)

Using photo factories, we can dry users and service factories. this is all.

FactoryBot.define do
  factory :photo, class: Faker::LoremFlickr do
    size { "900x900" }
    search_terms { Faker::Lorem.words }

    initialize_with do
      Faker::LoremFlickr.image(attributes)
    end
  end

  factory :user do
    username { Faker::DcComics.hero }
    password { "123456" }
    email { "#{username}@gmail.com" }
    bio { "Hi, I'm #{username}. #{Faker::Movie.quote}" }
    avatar { build(:photo, search_terms: ['dccomics']) }
  end

  factory :service do
    name { "#{Faker::Verb.ing_form} service" }
    description { "I'll use my #{Faker::Superhero.power} to accomplish the mission" }
    address { "#{Faker::Address.street_address}, #{Faker::Address.city}" }
    user
    photos {
      build_list(:photo, 3, search_terms: [Faker::Verb.ing_form])
    }
  end
end

Now, when you need a service to test, you can ask for one.

service = FactoryBot.create(:service)

Need a service by another name?

service = FactoryBot.create(:service, name: "Universal Exports")

What if there are no photos?

service = FactoryBot.create(:service, photos: [])

That's just fumbling around with what FactoryBot can do.

Factories are more flexible and convenient than torrent files.

Related


How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my $_Post to work

turquoise I'm trying to get my site to submit an email every time someone sends a message (now I'm struggling to get variables to work...). It's a learning process, so I'm giving it my all here, just trying to teach myself. But don't like global php commands $

How do I get my ListBox to work

Niermag I'm trying to get a listbox to work that will function very much like this. These Availableexercises are derived from Migration/Configuration. I need to be able to add multiples Availableto each exercise Selected Regime. I'm using viewmodels to access

How do I get my ListBox to work

Niermag I'm trying to get a listbox to work that will function very much like this. These Availableexercises are derived from Migration/Configuration. I need to be able to add multiples Availableto each exercise Selected Regime. I'm using viewmodels to access

How do I get my music bot to work on repl.it

Angelbros32: So I made a music bot on Repl.it and it worked fine until I flipped it over from a glitch to Repl. It played the song and everything. So when I move it to Repl everything works fine but the song doesn't play. This is not the you tube API that I ha

How do I get autocomplete (GoCode) to work with all my imports?

Paul: 在GoClipse中安装GoCode自动完成守护程序后,它可用于更一般的导入(fmt等),但不适用于更具体的导入。 我确实相信GoClipse的设置正确,因为它已经可以用于某些导入。我要使其起作用的特定导入是“ github.com/hyperledger/fabric/core/chaincode/shim”。 要使这些导入正常工作,必须做一些事情,但我还没有弄清楚。我总是可以在没有自动补全的情况下进行编码,但是。 有谁知道如何使它工作?谢谢你 注意:我会发布图片来说明我的问题,但是很好:“发布图片至

How do I get my music bot to work on repl.it

Angelbros32: So I made a music bot on Repl.it and it worked fine until I flipped it over from a glitch to Repl. It played the song and everything. So when I move it to Repl everything works fine but the song doesn't play. This is not the you tube API that I ha

How do I get autocomplete (GoCode) to work with all my imports?

Paul: After installing the GoCode autocomplete daemon in GoClipse, it works for more general imports (fmt, etc.), but not for more specific imports. I do believe that GoClipse is set up correctly as it already works for some imports. The specific import I'm tr

How do I get my framebuffer console to work?

Time4Tea I have an Apple MacBook running a Linux From Scratch system I've built. It's a minimal system that just boots into a bash prompt without installing the X Window System. Graphics chip is Intel GMA 950 using i915 driver. Previously, I had it boot into t

How do I get my keydown event to work?

Jesus Estevez I have a class Jugadorthat creates a new SVG rectangle and puts it on the canvas (ping pong game), I want to assign ArrowUp and ArrowDown to the first player and keys "W" and "A" for the second player to move "stamp". class Jugador { construc

How do I get my Java "if" statement to work?

Das Business I'm very new to learning Java and am currently working on a program that will allow me to play against a computer based on simple statistics assigned to us and a random number as a dice roll. I realize there may be many other issues with my code,

How do I get Firebase Phone Authentication to work with my app?

Charles Not sure what I'm doing wrong, I've searched on Stack Overflow and read articles and watched videos, but still no solution. Basically I want to send an SMS to the user's phone via Firebase to verify the phone number. My app is not on the play store or

How do I get my attribute filter to work?

User 10109713 I am using woocommerce and using attribute filter to filter my products by attribute. For example, I want to filter paints by their 10L property. I don't know how to make it work, can someone help me? User 10057926 This should work, it worked for

How do I get my HeightAnchor to work properly?

User 6421566 In my MainVC, I'm trying to constrain the UIView to be top, left, right and have a height of 80. Right now, my view is fullscreen. How would I fix my code to have the correct size? // variable var topViewCons : [NSLayoutConstraint] = [] // Constan

How do I get my custom kernel to work?

machine import numpy as np import sklearn.svm as svm Here is my kernel function def my_kernel(p1, p2): r = 1-np.dot(p1.T, p2) return np.exp(-r**2/4) and my data X1 = np.random.random((50,5)) X1 = X1/np.sum(X1) X1 = X1.T Y1 = [0, 0, 1, 1, 1] Then use

How do I get my jQuery code to work on all elements

Brown I have this accordion menu but it only works on the first menu ul. How can I make it work in all ulsuch environments ? I know it would be great in the future if you could explain what I'm doing wrong. Also, how do I get the link to switch between the two

How do I get my macro shortcuts to work again?

Lombardburn I created a macro to format the title of the spreadsheet and assigned the shortcut "Ctrl+Shift+H" to the macro. It was working fine initially, but now the shortcut key is no longer recognized. If I go to the macro dialog or the VBA editor, I can st

How do I get my electron auto-updater to work?

ScarVite I'm trying to get my Electron Vue.js app to update itself when I publish a new update in Github Repro. I am using "electron builder" to package my app, here is my package.json I'm following this guide and it didn't work. This is the code for the updat

How do I get my macro shortcuts to work again?

Lombardburn I created a macro to format the title of the spreadsheet and assigned the shortcut "Ctrl+Shift+H" to the macro. It was working fine initially, but now the shortcut key is no longer recognized. If I go to the macro dialog or the VBA editor, I can st

How do I get my autocomplete Chrome extension to work?

Stromback I've created a code that gives me different canonical answers and I always use it at work. The code is simple, but it would make me more efficient...if it works. The standard phrase has the tag name, if I open the browser I get a "userBox" and if I t