Model Error in CSV import - Ruby on Rails - ruby-on-rails

I've been racking my brain for a while now and I can't figure out why my csv upload in my rails app is failing. I have a simple model that converts two names in the csv to integers of foreign_ids. The model works completely fine when executed manually in the console but for some reason it fails on the server. I get the error message: undefined method `id' for nil:NilClass
The model looks as follows:
require 'csv'
class Schedule < ActiveRecord::Base
belongs_to :team
belongs_to :opponent, :foreign_key => 'opponent_id', :class_name => 'Team'
def self.import(file)
CSV.foreach(file.path, headers: true, :header_converters => :symbol) do |row|
week_hash = row.to_hash
teamname = week_hash[:team]
teamhash = Team.where(:name => teamname).first
teamhash_id = teamhash.id
week_newhash = week_hash.reject!{ |k| k == :team}
week_newhash[:team_id] = teamhash_id
opponentname = week_hash[:opponent]
opponent_hash = Team.where(:name => opponentname).first
hashopponent_id = opponent_hash.id
week_newhash = week_newhash.reject!{ |k| k == :opponent}
week_newhash[:opponent_id] = hashopponent_id
Schedule.create!(week_newhash)
end
end
end
The problem must be in here somewhere. Any help would be greatly appreciated. Thanks.

I'm an idiot. The model was fine I just had a column mislabeled in my csv.

Maybe change:
teamhash_id = teamhash.id
to:
teamhash_id = teamhash[:id], and hashopponent_id = opponent_hash.id to hashopponent_id = opponent_hash[:id]?

Related

How to convert string to existing attribute in model when creation

I got a array of strings, I want to retrieve for each the attribute during the creation of the post.
My array = ["_646_maturity", "_660_maturity", "_651_maturity", "_652_maturity", "_641_maturity"]
class Audit < ApplicationRecord
belongs_to :user
before_save :calculate_scoring
def calculate_scoring
scoring = []
models = ActiveRecord::Base.connection.tables.collect{|t| t.underscore.singularize.camelize.constantize rescue nil}
columns = models.collect{|m| m.column_names rescue nil}
columns[2].each do |c|
if c.include? "maturity"
Rails.logger.debug 'COLUMN : '+c.inspect
scoring.push(c)
end
end
getMaturity = ""
scoring.each do |e|
getMaturity = e.to_sym.inspect
Rails.logger.debug 'MATURITY : '+getMaturity
end
end
end
The log print > 'MATURITY : :_651_maturity'
I'm looking to the value of :_651_maturity who is a attribute of my post.
I tried .to_sym but it's not working..
Thanks for the help!
Inside calculate_scoring you can use self to point to the record you are saving. So self._651_maturity = <some_value>, self[:_651_maturity] = <some_value> and self['_651_maturity'] are all valid methods to set _651_maturity.
Also, you can do something like:
my_attrib = '_651_maturity'
self[my_attrib] = 'foo'

Create a record to the database without a form in Ruby on Rails 5

In my controller I have defined a method that I want to save to my database automatically without a form.
This is what I have so far, but nothing is being saved to the database.
Here's the method
def recommended_recipes
#today = Date.today
#recRecipe = RecommendedRecipe.where(user_id: current_user, day: #today)
if #recRecipe.blank?
response = RestClient.get("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/mealplans/generate?targetCalories=3000&timeFrame=day", headers={"X-Mashape-Key" => "",
"Accept" => "application/json"})
#parsedResponse = JSON.parse(response)
#recRecipes = #parsedResponse['meals']
#recRecipesNutrients = #parsedResponse['nutrients']
#totalCalories = #recRecipesNutrients['calories']
#totalProteins = #recRecipesNutrients['protein']
#totalFat = #recRecipesNutrients['fat']
#totalCarbohydrates = #recRecipesNutrients['carbohydrates']
#newRecRecipe = RecommendedRecipe.create(meals_response: #recRecipes, total_calories: #totalCalories, total_proteins: #totalProteins, total_fat: #totalFat, total_carbohydrates: #totalCarbohydrates, day: #today, user_id: current_user)
end
end
I want to save the #newRecipe to my database called recommended_recipes whenever the method is called.
How can I make a record in the database?
Thanks in advance!
After hours of kicking myself in the head I did this in the model:
class RecommendedRecipe < ApplicationRecord
belongs_to :user, optional: true
end
I added the optional: true

How can I refactor this Rails controller?

I have the following in my controller:
#custom_exercises = #user.exercises.all
#all_exercises = Exercise.not_the_placeholder_exercise.public.order("name").all
if #user.trainers.present?
trainer_exercises = []
#user.trainers.each do |trainer|
trainer_exercises << trainer.exercises.all
end
#my_trainer_custom_exercises = trainer_exercises
end
#exercises = #custom_exercises + #all_exercises
if #my_trainer_custom_exercises.present?
#exercises << #my_trainer_custom_exercises
#exercises.flatten!
end
This feels really messy. How could I refactor this?
First step: set up an AR relationship between users and exercises, probably along the lines of:
class User < ActiveRecord::Base
has_many :trainer_exercises,
:through => :trainers,
:foreign_key => :client_id,
:source => :exercises
end
Second step: move #all_exercises to a class method in Exercise.
class Exercise < ActiveRecord::Base
def self.all_exercises
not_the_placeholder_exercise.public.order("name").all
end
end
This way, the whole controller gets a whole lot simpler:
#custom_exercises = #user.exercises.all
#trainer_exercises = #user.trainer_exercises.all
#exercises = Exercise.all_exercises + #custom_exercises + #trainer_exercises
From a purely less lines of code perspective, you could start with this ( more or less / not tested but should work:
if #user.trainers.present?
#my_trainer_custom_exercises = #user.trainers.each.inject([]){ |trainer, trainer_exercises|
trainer_exercises << trainer.exercises.all
}
end

How can I make a delayed job for this custom method?

Here is my Lesson model:
#encoding: utf-8
class Lesson < ActiveRecord::Base
attr_accessible :content, :title, :parsed_content, :html_content, :user_id
serialize :parsed_content, Array
serialize :html_content, Array
serialize :pinyin_content, Array
serialize :defined_content, Array
serialize :literal_content, Array
validates :title, :presence => true
validates :content, :presence => true
belongs_to :user
before_update do |lesson|
lesson.makesandwich
end
before_save do |lesson|
lesson.delay.makesandwich
end
def makesandwich
require 'rmmseg'
#require 'to_lang'
require 'bing_translator'
require 'ruby-pinyin'
self.parsed_content = []
RMMSeg::Dictionary.load_dictionaries
content = self.content
paragraphs = content.split(/\r\n\r\n/) #convert to array of paragraphs
self.parsed_content = paragraphs
paragraphs.each_with_index do |text, ti|
text = text.gsub("。", "^^.")
text = text.gsub("?", "~~?")
text = text.gsub("!", "||!")
text = text.gsub(":", ":") #fix missing colons
text = text.split(/[.?!]/u) #convert to an array
text.each do |s|
s.gsub!("^^", "。")
s.gsub!("~~", "?")
s.gsub!("||", "!")
#s.gsub!("———————————",":")
end
text.each_with_index do |val, index|
algor = RMMSeg::Algorithm.new(text[index])
splittext = []
loop do
tok = algor.next_token
break if tok.nil?
tex = tok.text.force_encoding('UTF-8')
splittext << tex
text[index] = splittext
end
paragraphs[ti] = text
end
end
bing = BingTranslator.new(BING_API)
self.parsed_content = paragraphs
textarray = Marshal.load(Marshal.dump(paragraphs))
self.defined_content = Marshal.load(Marshal.dump(paragraphs))
self.literal_content = Marshal.load(Marshal.dump(paragraphs))
self.pinyin_content = Marshal.load(Marshal.dump(paragraphs))
textarray.each_with_index do |paragraph, pi|
paragraph.each_with_index do |sentence, si|
sentence.each_with_index do |word, wi|
if DictionaryEntry.find_by_simplified(word) != nil
self.defined_content[pi][si][wi] = DictionaryEntry.find_by_simplified(word).definition
#self.literal_content is down below
self.pinyin_content[pi][si][wi] = DictionaryEntry.find_by_simplified(word).pinyin
else
self.defined_content[pi][si][wi] = bing.translate(word, :from => 'zh-CHS', :to => 'en')
#self.defined_content[pi][si][wi] = word
#self.literal_content is down below
if PinYin.of_string(word, true).length > 1 #for punctuation
self.pinyin_content[pi][si][wi] = PinYin.of_string(word, true).join(" ").downcase
else
self.pinyin_content[pi][si][wi] = word
end
end
end
end
end
#Literal
literalarray = Marshal.load(Marshal.dump(paragraphs))
literalarray.each_with_index do |paragraph, pi|
paragraph.each_with_index do |sentence, si| #iterate array of sentence
literalarray[pi][si] = []
sentence.each_with_index do |word, wi| #iterate sentence's array of words
entrytobesliced = DictionaryEntry.find_by_simplified(word)
slicedentry = []
if entrytobesliced == nil
if word.length > 1 && word !~ /\w/ #/^\s*\w\d+\s*$/ #number regex #for cases where there is no DictionaryEntry
split = []
wordarray = word.split("").each_with_index() do |ws, wsi|
split << [DictionaryEntry.find_by_simplified(ws).definition]
end
literalarray[pi][si] << split
else
literalarray[pi][si] << [word] #in case none of the above work
end
else
entrytobesliced.simplified.each_char do |w|
singlechar = DictionaryEntry.find_by_simplified(w)
slicedentry << singlechar.definition.split("\", \"")
end
literalarray[pi][si] << slicedentry
end
self.literal_content = literalarray #slicedentry #literalarray
end
end
end
end
end
When I try to create a new lesson it errors like this: Jobs cannot be created for records before they've been persisted
But if I change it to after_save instead of before_save then I can see the work run, but it doesn't update the serialized arrays in the database.
Can someone please help me implement delayed_jobs for this? It was working when I had:
before_save do |lesson|
lesson.makesandwich #no delay
end
I think you're getting these errors:
Jobs cannot be created for records before they've been persisted
because your Lesson instances won't have an id until they've been saved and without an id, DJ has no way to know which instance it should be working with. So you have to use an after_save so that your Lesson has an id and can be uniquely identified. But then your updates from the delayed job won't be saved because, well, nothing asks for them to be saved. You should be able to get around that simply by adding a self.save or self.save! call at the end of makesandwich.

Ways to simplify and optimize my code?

I've got some code which i would like to optimize.
First, not bad at all, but maybe it can be a bit shorter or faster, mainly the update_result method:
class Round < ActiveRecord::Base
belongs_to :match
has_and_belongs_to_many :banned_champions, :class_name => "Champion", :join_table => "banned_champions_rounds"
belongs_to :clan_blue, :class_name => "Clan", :foreign_key => "clan_blue_id"
belongs_to :clan_purple, :class_name => "Clan", :foreign_key => "clan_purple_id"
belongs_to :winner, :class_name => "Clan", :foreign_key => "winner_id"
after_save {self.update_result}
def update_result
match = self.match
if match.rounds.count > 0
clan1 = match.rounds.first.clan_blue
clan2 = match.rounds.first.clan_purple
results = {clan1=>0, clan2=>0}
for round in match.rounds
round.winner == clan1 ? results[clan1] += 1 : results[clan2] += 1
end
if results[clan1] > results[clan2] then
match.winner = clan1; match.looser = clan2
match.draw_1 = nil; match.draw_2 = nil
elsif results[clan1] < results[clan2] then
match.winner = clan2; match.looser = clan1
match.draw_1 = nil; match.draw_2 = nil
else
match.draw_1 = clan1; match.draw_2 = clan2
match.winner = nil; match.looser = nil
end
match.save
end
end
end
And second, totally bad and slow in seeds.rb:
require 'faker'
champions = [{:name=>"Akali"},
{:name=>"Alistar"},
{:name=>"Amumu"},
{:name=>"Anivia"},
{:name=>"Annie"},
{:name=>"Galio"},
{:name=>"Tryndamere"},
{:name=>"Twisted Fate"},
{:name=>"Twitch"},
{:name=>"Udyr"},
{:name=>"Urgot"},
{:name=>"Veigar"}
]
Champion.create(champions)
10.times do |n|
name = Faker::Company.name
clan = Clan.create(:name=>name)
6.times do |n|
name = Faker::Internet.user_name
clan.players.create(:name=>name)
end
end
for clan in Clan.all do
2.times do
match = Match.create()
c = [clan,Clan.first(:offset => rand(Clan.count))]
3.times do
round = match.rounds.create
round.clan_blue = c[0]
round.clan_purple = c[1]
round.winner = c[0]
round.save!
end
for item in c
for p in item.players.limit(5)
rand_champion = Champion.first(:offset => rand(Champion.count))
match.participations.create!(:player => p, :champion => rand_champion)
end
end
match.save!
end
2.times do
match = Match.create()
c = [clan,Clan.first(:offset => rand(Clan.count))]
3.times do
round = match.rounds.create
round.clan_blue = c[0]
round.clan_purple = c[1]
round.winner = c[1]
round.save!
end
for item in c
for p in item.players.limit(5)
rand_champion = Champion.first(:offset => rand(Champion.count))
match.participations.create!(:player => p, :champion => rand_champion)
end
end
match.save!
end
2.times do
match = Match.create()
c = [clan,Clan.first(:offset => rand(Clan.count))]
2.times do |n|
round = match.rounds.create
round.clan_blue = c[0]
round.clan_purple = c[1]
round.winner = c[n]
round.save!
end
for item in c
for p in item.players.limit(5)
rand_champion = Champion.first(:offset => rand(Champion.count))
match.participations.create!(:player => p, :champion => rand_champion)
end
end
match.save!
end
end
Any chances to optimize them?
Don't underestimate the value of whitespace in cleaning up code readability!
class Round < ActiveRecord::Base
belongs_to :match
belongs_to :clan_blue, :class_name => "Clan", :foreign_key => "clan_blue_id"
belongs_to :clan_purple, :class_name => "Clan", :foreign_key => "clan_purple_id"
belongs_to :winner, :class_name => "Clan", :foreign_key => "winner_id"
has_and_belongs_to_many :banned_champions, :class_name => "Champion", :join_table => "banned_champions_rounds"
after_save { match.update_result }
end
class Match < ActiveRecord::Base
def update_result
return unless rounds.count > 0
clan1, clan2 = rounds.first.clan_blue, rounds.first.clan_purple
clan1_wins = rounds.inject(0) {|total, round| total += round.winner == clan1 ? 1 : 0 }
clan2_wins = rounds.length - clan1_wins
self.winner = self.loser = self.draw_1 = self.draw_2 = nil
if clan1_wins == clan2_wins
self.draw1, self.draw2 = clan1, clan2
else
self.winner = clan1_wins > clan2_wins ? clan1 : clan2
self.loser = clan1_wins < clan2_wins ? clan1 : clan2
end
save
end
end
For your seeds, I'd replace your fixtures with a factory pattern, if it's for tests. If you're going to stick with what you have there, though, wrap the whole block in a transaction and it should become orders of magnitude faster.
Well, on your first example, it appears that you are forcing Match behavior into your Round class, which is not consistent with abstract OOP. Your update_result method actually belongs in your Match class. Once you do that, I think the code will clean itself up a bit.
On your second example, it's hard to see what you are trying to do, but it's not surprising that it's so slow. Every single create and save generates a separate database call. At first glance your code generates over a hundred separate database saves. Do you really need all those records? Can you combine some of the saves?
Beyond that, you can cut your database calls in half by using build instead of create, like this:
round = match.rounds.build
round.clan_blue = c[0]
round.clan_purple = c[1]
round.winner = c[0]
round.save!
If you want to save some lines of code, you could replace the above with this syntax:
match.rounds.create(:clan_blue_id => c[0].id, :clan_purple_id => c[1].id, :winner_id => c[0].id)
In your seeds file:
c = [clan,Clan.first(:offset => rand(Clan.count))]
This works, but it looks like you're picking a random number in Ruby. From what I understand, if you can do something in SQL instead of Ruby, it's generally faster. Try this:
c = [clan,Clan.find(:all, :limit => 1, :order => 'random()')
You won't get too many gains since it's only run twice per clan (so 20x total), but there are similar lines like these two
# (runs 60x total)
rand_champion = Champion.first(:offset => rand(Champion.count))
# (runs up to 200x, I think)
c = [clan,Clan.first(:offset => rand(Clan.count))]
In general, you can almost always find something more to optimize in your program. So your time is most efficiently used by starting with the areas that are repeated the most--the most deeply nested loops. I'll leave optimizing the above 2 lines (and any others that may be similar) to you as an exercise. If you're having trouble, just let me know in a comment.
Also, I'm sure you'll get a lot of good suggestions in many of the responses, so I highly highly highly recommend setting up a benchmarker so you can measure the differences. Be sure run it several times for each version you test, so you can get a good average (programs running in the background could potentially throw off your results).
As far as simplicity, I think readability is pretty important. It won't make your code run any faster, but it can make your debugging faster (and your time is important!). The few things that were giving me trouble were nondescript variables like c and p. I do this too sometimes, but when you have several of these variables in the same scope, I very quickly reach a point where I think "what was that variable for again?". Something like temp_clan instead of c goes a long way.
For readability, I also prefer .each instead of for. That's entirely a personal preference, though.
btw I love League of Legends :)
Edit: (comments won't let me indent code) Upon taking a second look, I realized that this snippet can be optimized further:
for p in item.players.limit(5)
rand_champion = Champion.first(:offset => rand(Champion.count))
match.participations.create!(:player => p, :champion => rand_champion)
end
change Champion.first(:offset => rand(Champion.count))
rand_champs = Champion.find(:all, :limit => 5, :order => 'random()')
for p ...
i = 0
match.participations.create!(:player => p, :champion => rand_champs(i))
i++
end
This will reduce 5 SQL queries into 1. Since it's called 60x, this will reduce your SQL queries from 60 to 12. As an extra plus, you won't get repeated champions on the same team, (or I guess that could be a downside if that was your intention)

Resources