How to Update All of My Products to a Specific User When Seeding

Can I update all of my products to a specific user when seeding?

one naive way to do this is :

walmartprices.each{|record| record.update_attribute(user: admin) }

for better performance, use update_all :

Product.where( id: walmartprices.map(&:id) ).update_all( user: admin )

Rails - Updating a value through rails seed for a given user

Since it's a belongs_to it's only one homecity and it's actually the homecity_id you're trying to pass to devise.

So instead of...

class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar, :homecities])
devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar, :homecities])
end
end

You should do...

class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar, :homecity_id])
devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar, :homecity_id])
end
end

And you'll need to modify your edit view to the singular...

<%= f.collection_select :homecity_id, Homecity.all, :id, :Hometown %>

Finally, do rake db:rollback and change homecities to homecity in your migration and then do rake db:migrate again.

Seeding method is inserting additional Entities with NULL values

EDIT
I have created a test app to just be able to test things out and be 100%.
I do not yet know why but i have the same relation as before (Price Entities only having 1 Product reference (that was creating a duplicate)) and i don't have a duplicate...

So i can have the relation i want which is 1 Price should only have 1 Product reference but i have absolutely no idea what is happening here ...

Changing the relation in the PurchasePrice Class of the Entity Product to a ICollection instead of 1 Single Product doesn't create a dupe (and creates a PurchasePriceProduct table).

Seems from the database logs (log4net) that due to the relation EF is first inserting a Product (NULL) for the PurchasePrice Reference of the Product , AND inserts the Product (NOT NULL) with its references ... (If anyone needs any clarification on this let me know ill do my best)

This post HAS MOVED TO HERE.
I want to thank everyone that has contributed in any way!

How to create a default login user account when seeding users?

In your UserSeeder.php create a default user (using your factory) just before you seed your other users:

public function run()
{
// Create admin user
$user = factory(User::class)->create([
'name' => 'AdMiN',
'email' => 'admin@admin.com',
'password' => bcrypt('aDmIn'),
]);

// Create 5 more users:
factory(User::class, 5)->create();
}

Note: The create() method from factories can take an array of attributes. These will overwrite the definitions from the factory.

Make sure to activate/run this seeder in your DatabaseSeeder.php:

public function run()
{
$this->call(UserSeeder::class);
}

Edit: there should be no need for your DefaultUserFactory.php, everything can be handled via your UserFactory.php

Entity framework migration and seeding specific update

You can run a specific migration by specifying the name of the migration. For example, If you have a migration called MyTuesdayMigration.cs, In the package manager console, you would run this command:

update-database -TargetMigration MyTuesdayMigration

Which is better for database seeding: Add or AddOrUpdate?

I assume that Style's primary key is Id. The overload of AddOrUpdate that you use only checks if there is a record having Id == 1. If so, it updates it. That's all.

What's going wrong here is that the primary key is a surrogate key, i.e. it's there for querying convenience, but it's got no business meaning. Usually, with migrations you want to look for the natural keys of entities though. That's how the user identifies data. S/he wants a green style, not a style identified by 1.

So I think you should use this overload of AddOrUpdate:

context.Styles.AddOrUpdate( s => s.Color,
new Style { Id = 1, Color = "red" });

Now when there is no red style anymore, a new one is inserted, overriding the Id value (assuming that it's generated by the database).

From your later comments I understand that you want to Add data when they're new, but not update them when they exist (compared by primary key). For this you could use a slightly adapted version of an AddWhenNew method I described here. For your case I would do it like so:

public T void MarkAsAddedWhenNew<T>(this DbContext context, 
Expression<Func<T, object>> identifierExpression, T item)
where T : class
{
context.Set<T>().AddOrUpdate(identifierExpression, item);
if (context.Entry(item).State != System.Data.Entity.EntityState.Added)
{
var identifierFunction = identifierExpression.Compile();
item = context.Set<T>()
.Local
.Single(x => identifierFunction(item)
.Equals(identifierFunction(x)));
context.Entry(item).State = System.Data.Entity.EntityState.Unchanged;
}
return item;
}

Re-fetching the item from the local collection is a nuisance, but necessary because of a bug in AddOrUpdate(). This bug also caused the error you got when setting the state of the original entry to Unchanged: it was a different instance than the attached one.

Seeding users with Devise in Ruby on Rails

You have to do like this:

user = User.new
user.email = 'test@example.com'
user.encrypted_password = '#$taawktljasktlw4aaglj'
user.save!

Read this guide to understand what mass-assignment is: http://guides.rubyonrails.org/security.html

I am wondering why do have to directly set the encrypted password. You could do this:

user.password = 'valid_password'
user.password_confirmation = 'valid_password'


Related Topics



Leave a reply



Submit