last day (16 days later) » 

10:17 PM
1
A: Create FactoryBot User with has_one relationship

srngIn order to create dependent Factories you have to create a factory for each model, and then just add the dependent Model name to your factory, ie. FactoryBot.define do factory :user, class: User do first_name { "John" } last_name { "Doe" } email { Faker::Inter...

 
When I try the above, ActiveRecord::RecordInvalid:, Validation failed: Stylable must exist, NOTE: Style has a polymorphic association with Car. Style has stylable_type, and Stylable_id. Any thing special I need to do?
tried your example and get same error: ActiveRecord::RecordInvalid: Validation failed: Stylable must exist
 
See changed answer, I've tested it locally on mine and got no errors so it is being created. I'm not sure about the mass of private methods you've got though.
 
What version of rails are you using?
 
5.2.2. Did you get another error?
 
Yes, what do you models look like? are you using the new optional parameter in the model?
I realized I got a slightly different error, User must exist now.
 
10:17 PM
Hm, well I deleted all the example I coded, and when I just rewrote them it suddenly broke... Now I've got it back but with a different solution. I'll post all the relevant code. I didn't include the user because the polymorphic Style -> Car relationship is the one giving trouble.
What was the new error?
 
Hi, Thank you so much for helping
I have spent two days on this
You said you got it working again? with another version?
 
No worries. I'm interested to solve it. Let me know if the code matches up with yours.
 
did you post the new code?
 
Yah, I edited the answer with all my models, migrations etc.
 
reviewing it now and will test shortly
 
10:32 PM
Same user must exist error
I compared you code to mine, did not see anything different
mind posting the git repo and I can take a look?
maybe something else is different?
did you change any of the configurations?
 
same user must exist? Can you post the full error message here?
and the failing test?
 
CarRegistration::Selling#initialize with one missing field it returns an error for boolean field returns error when has_wheels is missing
Failure/Error: @user = create(:user)

ActiveRecord::RecordInvalid:
Validation failed: User must exist
Another error:
CarRegistrationController When user loggedin GET #show steps When Good Data Requesting with first step INFO should return success
Failure/Error: user = FactoryBot.create(:user)

ActiveRecord::RecordInvalid:
Validation failed: User must exist
I am thinking one of my associations with User is off, but not sure
OH, did FactoryBot.define do
factory :style, class: Style do
style_number { "Blue" }
end

factory :car, class: Car do
name { "Holden" }
trait :style do
association :stylable, factory: :style
end
end all have to be in one file?
 
hmmm
 
I have each in separate files
a style.rb file and a car.rb file
 
nah, as long as they are in the FactoryBot define block
 
10:42 PM
hummm...
 
thats good. Whats your userfactory like?
 
FactoryBot.define do
factory :user, class: User do
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
password { "password" }
password_confirmation { "password"}
car
end
end
Could this be causing an issue:
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryBot.create(:user)
sign_in user
end
end

end
I use it for some tests
 
its not creating a user. whats the user_params method look like in your user_controller
I don't think so, youre not using it for these ones are you?
 
  user = FactoryBot.create(:user), references it
don't have a user_controller, it's the standard devise user_controller
class RegistrationsController < Devise::RegistrationsController
protected

# https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)

private

def after_sign_up_path_for(resource)
search_index_path(resource)
end

def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :location, type_of_firms:[], interest_options:[])
end

def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :location
This is my registration controller, but I don't think it will be helpful
 
can you post the devise_create_user migration please
and the user schema
 
10:52 PM
# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""

## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at

## Rememberable
t.datetime :remember_created_at

## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.jsonb "interests", default: {}, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
 
aha,
 
find something?
 
user isn't being created because it's silently failing. You are passing it an unpermitted parameter.
The Car
There's no user field for Car.
 
User has_one Car. So Car would have the user_ID
Car belongs_to User
am I off?
my Car table has Use_id
I need to step away, not sure when I can hope back on, my two year old is sick and our sitter is feeling il now. I will monitor this as I get a chance. Let me know if I can provide anything else that may be helpful. If you post a git repo of the demo app you tried, I can review. AND THANK YOU FOR HELPING! I REALLY APPRECIATE IT!
 
but you are passing it in as a parameter in the Factory.
 
10:58 PM
I am?
oh Car?
 
User.FactoryBot.define do
factory :user, class: User do
first_name { "John" }
last_name { "Doe" }
email { Faker::Internet.email }
password { "password" }
password_confirmation { "password"}

end
end
 
So I do I reverse that?
 
Get rid of car, and then I'm assuming do an after create block like the other one
 
But User has_one Car
I tried that and if failed, but I will try again.
will let you know when I do. Thank you!
 
kk
I'll add a user block to the answer
 
11:32 PM
Added the block to user and now get ActiveRecord::RecordInvalid:
Validation failed: Car must exist
 
in both tests again?
ooooh, probably the :user => user line. It's annoying the errors aren't super informative. try :user_id instead, might have to change it to user.id as well.
 
CarRegistration::Base#save Validating with good data calling it should return true
Failure/Error: @style = create(:style , stylable: @Car)

ActiveRecord::RecordInvalid:
Validation failed: Car must exist
made the User_id change
 
11:50 PM
Whats the test there? For the registration.
Shouldn't it be @style = create(:car).style
or the like
 

  last day (16 days later) »