I'm using a rake task to populate my database with some initial data. I want to create a bunch of entries in a table, with the first few IDs, so they're always present, and always have those ids. I don't mind if in a dev environment, someone adds/deletes/modifies records, but I always want the first 5 ids to have values. Here's a simplified version of my lib/tasks/bootstrap.rb file:
namespace :bootstrap do
desc "Create the default problem types"
task :default_problem_types => :environment do
ProblemType.create( :id => 1, :name => 'Wrong location', :description => 'blah' )
ProblemType.create( :id => 2, :name => 'Wrong name', :description => 'blah' )
ProblemType.create( :id => 3, :name => 'Wrong details', :description => 'blah' )
ProblemType.create( :id => 4, :name => 'Duplicate', :description => 'blah' )
ProblemType.create( :id => 5, :name => 'No longer exists', :description => 'blah' )
end
desc "Run all bootstrapping tasks"
task :all => [:default_problem_types]
end
This works fine on an empty database. It creates 5 new entries in the problem_types table:
1 - Wrong Location
2 - Wrong name
3 - Wrong details
4 - Duplicate
5 - No longer exists
The problem is that if I run it a second time, it creates 5 new records, with IDs 6, 7, 8, 9, 10. That's despite the fact that I provided ids to the create() call that already exist. I'm expecting those calls to fail, because if I try to do the following SQL:
insert into problem_types (id, name, description) values (1, 'foo', 'bar');
... it fails:
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
How do I get the create() method to fail if the ID already exists?
Thanks.
Use a dynamic find_or_create_by or find_or_initialize_by method. You can read more about these in the Rails guides for the Active Record query interface. They allows you to write something like this for each ProblemType without creating duplicates by name:
problem_type = ProblemType.find_or_initialize_by_name('Wrong location')
problem_type.description = "blah"
problem_type.save
I agree with #benchwarmer; it's best to avoid managing the primary key yourself. If you must have a numerical identifier that some semantic value, try adding a separate column and set that value accordingly.
Rule of thumb: You dont assign ID on your own. Let database do its work. If you want your problem names to be unique then you can add validates_uniqueness_of :name into model file and the records won't be created if the same name exists
Related
I'm successfully following some of the commands listed here and applying them to my own tables.
How do I turn the output into actual records/data ? Presently it's returning some output which I can assign to a variable, but I do not understand this output at all.. it doesn't look like an array or hash (but perhaps it is), and I do not understand how to access its contents (I want to use the output in a rails view, but at the least would like to access the table records in the console where I'm executing the Arel code)
For example, say I have two tables; User and Product as follows:
User = [{:id => 1, :name => "Joe", :email => "joe#gmail.com"}, {:id => 2, :name => "Jane", :email => "jane#gmail.com"}]
Product = [{:product_name => "Car Tire", :brand => "BMW", :last_edited_by => 1}, {:product_name => "Paint Brush", :brand => "Dulux", :last_edited_by => nil}, {:product_name => "Dog Biscuits", :brand => "Bowow Snacks", :last_edited_by => 2}, {:product_name => "Game", :brand => "Westwood", :last_edited_by => 1}]
Suppose I want to join the two tables User.id = Product.last_edited_by
In SQL it would be as simple as:
SELECT *
FROM User
JOIN Product
WHERE users.id = products.last_edited_by
Now if I plug this into scuttle.io to convert to Arel, it gives:
User.select(Arel.star).where(
User.arel_table[:id].eq(Product.arel_table[:last_edited_by])
).joins(
User.arel_table.join(Product.arel_table).on(null).join_sources
)
But trying this returns: ArgumentError: wrong number of arguments (given 1, expected 0)
Attempting to emulate advice given here, I come up with another approach:
users.project(Arel.star).join(User).on(Product[:last_edited_by].eq(User[:id]))
But this returns: NoMethodError: undefined method `project' for Array..
A final point, if I run
users = Arel::Table.new(User)
output = users.project(users[:id].as("id"), users[:id].count.as("count")).group("id")
I get some output which looks almost meaningful, but I don't know how to return that into something I can make sense of, and I wouldn't know how to use it if I provided it to a rails view
Firstly, I'm really sorry to ask such a basic question but I'm very new to Rails, Ruby & development in general and my lack of terminology understanding means that I am having a hard time finding the answer to this question.
I am importing a csv of vehicle data using:
def self.import(file, category_id)
CSV.foreach(file.path, headers: true) do |row|
Model.where(
:category_id => category_id,
:name => row[1],
:cap_id => row[10]
).first_or_create do |record|
record.layout = row[3],
... several records later ...
record.manufacturer_id = Manufacturer.where(:name => row[0], :category_id => category_id).id
end
end
end
but I am having trouble setting the manufacturer_id with the last line of the loop. I guess you can see what i'm trying to do here but I can't work out how this should be written.
Basically i have a list of manufacturers belonging to different categories (so, for example, there is a Ford manufacturer with category_id=1 (for cars) and another Ford manufacturer with category_id=54 for trucks.
Can I set the manufacturer_id of each record by taking the current category_id and the manufacturer name from the csv, cross referencing them and pulling back the id of the result?
When trying the above code i get the following error:
NoMethodError in ModelsController#import
undefined method `id' for #<ActiveRecord::Relation::ActiveRecord_Relation_Manufacturer:0x007fcd96f744a8>
Extracted source (around line #33):
32 record.description = row[2],
33 record.manufacturer_id = Manufacturer.where(:name => row[0], :category_id => category_id).id
34 end
35 end
36 end
The following line of code returns an ActiveRecord::Relation object
Manufacturer.where(:name => row[0], :category_id => category_id)
so when you called id on it, it raised an error. You should call first first and then id.
Manufacturer.where(:name => row[0], :category_id => category_id).first.id
Be warned that doing so assumes that you'll always get a manufacturer that matches the condition. To be more safe, you can use try
Manufacturer.where(:name => row[0], :category_id => category_id).first.try(:id)
I need help in populate some random data in my database table.
I have a list of 10 users in my system. My allergy table has the following fields:
id user_id name reactions status
I have the following allergies hash in a variable called allergy_hash.
{:reaction_name=>"Bleeding", :status=>"Death", :name=>"A"} {:reaction_name=>"Nausea", :status=>"Serious", :name=>"B"} {:reaction_name=>"Fever", :status=>"Death", :name=>"C"} {:reaction_name=>"Blistering", :status=>"Serious", :name=>"D"}
Here is what I have done so far:
def create_random_data
users.each do |user|
allergies.each do |allergies_hash|
Allergy.where(user_id: user.id).first_or_create(
allergies_hash )
end
end
end
What the above does is just inserts Bleeding, Death and A into the table for all users 1 to 10.
But I need to insert such that different users can have different values. Also some users can have more than one allergy and the associated reactions.
NOTE: I do not mean completely random. For example name 'A' should still have the associated status 'Death' and reaction_name 'Bleeding'.
Name 'B' should have the associated status 'Serious' and reaction 'Nausea'in the allergy table.
When creating the users, use sample on allergies_hash = [{:reaction_name=>"Bleeding", :status=>"Death", :name=>"A"}, {:reaction_name=>"Nausea", :status=>"Serious", :name=>"B"}, {:reaction_name=>"Fever", :status=>"Death", :name=>"C"}, {:reaction_name=>"Blistering", :status=>"Serious", :name=>"D"}]
Allergy.where(user_id: user.id).first_or_create(allergies_hash.sample)
UPDATE
I'll loop through the users instead, so for each user you attempt to add from 1 to 3 allergies from your allergies_hash
User.all.each do |user|
[1,2,3].sample.times do
user.allergies.where(allergies_hash.sample).first_or_create
end
end
I would recommend you to check Faker and Factory girl to populate some random data.
You can either seed data into your app by going to the seed file in the app/db directory
and do something like this
User.delete_all
Bill.delete_all
u1 = User.create(:email => "bob#aol.com", :password =>"a", :password_confirmation => "a")
b1 = Bill.create(:name => "rent", :description => "the rent", :amount => 10_000, :day => 1)
b2 = Bill.create(:name => "cable", :description => "the cable", :amount => 150, :day => 5)
or you can also use the Faker gem to generate fake data.
http://geekswithblogs.net/alexmoore/archive/2010/01/18/faker-gem---a-quick-and-dirty-introduction.aspx
I have some trouble with mongoid:
test "Test candidate" do
User.create(:id => 1, :sex => User::Male, :country => 1, :city => 1)
User.create(:id => 2, :sex => User::Female, :country => 1, :city => 1)
User.create(:id => 3, :sex => User::Female, :country => 1, :city => 1)
user = User.not_in(:id => [2]).second
assert_not_equal(user.id, 2)
end
Test failed. I've tried to use where(:id => {'$nid' => [2]}), but it have same effect.
What is wrong? How to use "not in" condition with mongoid?
PS, "second" is ok, with "first" test passed, because id=1
Try this query:
user = User.not_in(:_id => [2]).second
In MongoDB primary key has name _id. Mongoid tries to be friendly and partially hides this fact from the developer by aliasing it to id in the object model. But when you do queries, it cannot tell if you want primary key _id or some completely ordinary field id.
user = User.where(:id.nin => [2,3,4,5])
This is as per mongoid official doc : http://mongoid.org/en/origin/docs/selection.html
I would like to seed my Products and assign them to a specific User and Store.
Product.rb
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :store
def product_store=(id)
self.store_id = id
end
end
Note: Store belongs_to Business (:business_name)
Seed.rb
This is my basic setup:
user = User.create(:username => 'user', :email => 'user2#email.com')
store = Store.create(:business_name => 'store', :address => 'Japan')
I attempted these but they did not work:
# This gives random ID's ranging from 1 to 4425!?
user.products.create([{:name => "Apple", :product_store => Store.find_by_address('San Francisco, USA')}])
# This gives me undefined method 'walmart'.
user.store.products.create([ {:name => "Apple"} ])
Is there a way to set the ID's so I can associate my Products to a Store and User?
UPDATE -
I have tried the answers below and still came out unsuccessful. Does anyone know of another way to do this?
Although it sounds like you found a workaround, the solution may be of interested to others.
From your original seeds.rb
user = User.create(:username => 'user', :email => 'user2#email.com')
store = Store.create(:business_name => 'store', :address => 'Japan')
Create the store
Store.create({
user_id: user.id
store_id: store.id
}, without_protection: true)
In the original code snipped "user" and "store" variables are declared. The code assigns user_id / store_id (the model columns inferred by the belongs_to relationship in the Store model) to the id values that are present in the "user" and "store" variables.
"without_protection: true" turns off bulk assignment protection on the id fields. This is perfectly acceptable in a seeds file but should be used with extreme caution when dealing with user provided data.
Or alternatively create your stores.
Then extract the correct one
e.g.
store = Store.find_by_business_name('Test Store')
and then create it based on that
e.g.
store.products.create(:product_name => "Product Test", :price => '985.93')
This will then set the relationship id for you,
If I'm not mistaken, you're just trying to do this.
user = User.create(:username => 'usertwo', :email => 'user2#email.com')
walmart = Store.create(:business_name => 'Walmart', :address => 'San Francisco, USA')
user.products.create(:name => 'Apple', :store => walmart)
Anything else required here that I'm not seeing?
Try doing this
store_1 = Store.new(:business_name => 'Test Store',
:address => 'Test Address',
:phone_number => '555-555-555')
store_1.id = 1
store_1.save!
The trick is not to set the id within the hash as it is protected.
Scott
What I did was update the particular products to a certain user, see this question:
Can I update all of my products to a specific user when seeding?
You could just create a series of insert satements for this "seed migration", including the record Id for each user, store, product etc. You might have to update database sequences after this approach.
Another approach
Create the initial records in you Rails app, through the GUI / web.
Then use something like Yaml-db. So you can dump the data to a yaml file. You can now edit that file (if necessary) and use that same file to seed another instance of the db with "rake db:load"
Either way.... You know the Ids will not be shifting around on you when these objects are created in the new db instance.
I'm sure there are other ways to do this... Probably better ones, even.
Here is a link to a write-up I did a while back for using yaml_db to seed an oracle database
http://davidbharrison.com/database_seeding_oracle
Try this:
User.destroy_all
Product.destroy_all
user = User.create!([{:username => 'usertwo', :email =>'user2#email.com'},
{:username => 'userthree', :email => user3#email.com}])
user.each_with_index do |obj, index|
Product.create!([{ :product_name => 'product #{index}', :user_id => obj.id }])
end
The table would look like this:
Here's how I prefer to seed an association in rails 6
#teacher = Teacher.new(name: "John")
#student = #teacher.build_student(name: "Chris")
#teacher.save!
#student.save!