Seed turn

I had a problem visiting my database using seed.rb , in particular where the table relationships are related.

Here is a sample code:

 # seed.rb user = User.find_or_create_by_login( :login => "myname", :email => " myname@gmail.com ", :user_type => "Admin", :password => "admin", :password_confirmation => "admin") project = Project.find_or_create_by_user_id( :user_id => user.id, :name => "Test Project") 

When the project is created (along with other unrelated parameters that I did not consider above), user_id is empty. How can I make this work?


This is the strangest behavior I have seen in everything that is so simple. About eight tables have been created in my seed file, and some of them are nested at 3-4 levels (i.e. users have several projects, projects have several tasks, etc.).

When I call user user as above and the user.id link several times after that, it only works once! I tried adding [user.reload] before creating each new entry, but to no avail. I do not think that this will make sense to everyone, but are there any opportunities here? Thanks to everyone.

+4
source share
5 answers

I realized what the problem is. Fields that were not populated are not explicitly specified in attr_accessible in their respective models. The fields listed were saved correctly.

Many thanks for your help.

+5
source

The code is fine and is the correct syntax for find_or_create . As others have said, the most likely problem is that the user is invalid. Attempting to call user.reload will cause it to explode if the user is invalid, and this will make the problem more obvious, but the error you get from it will be useless (it will moan about the inability to find the user without an identifier).

Unfortunately, find_or_create does not work as a bang method to throw exceptions if it is invalid, so it’s best to make a mistake and throw an error after trying to create a user:

 user = User.find_or_create_by_login(:login => "myname") raise "User is invalid: #{user.errors.full_messages}" unless user.valid? 
+3
source

User created with success? if so .. try user.reload if not. this is probably a mistake

0
source

Are you sure your user is saved? I think the correct syntax for find_or_create_by_XX is Blog.find_or_create_by_title("Some Blog") . If you need to transfer more data, you need to use find_or_initialize first and set other data after that separately.

Personally related branch: Rails find_or_create with more than one attribute?

- change

Passing data as a hash on find_or_create_by_XX seems to work too. The documents are in the Attribute-Based Dynamic Crawlers section here http://apidock.com/rails/v3.0.0/ActiveRecord/Base

0
source

try this use User.find_or_create instead of User.find_or_create_by_login . It looks like your custom object is not being saved. Or, before assigning user.id, do user.reload

 user = User.find_or_create( :login => "myname", :email => " myname@gmail.com ", :user_type => "Admin", :password => "admin", :password_confirmation => "admin") [user.reload] project = Project.find_or_create_by_user_id( :user_id => user.id, :name => "Test Project") 
0
source

Source: https://habr.com/ru/post/1333893/


All Articles