Rails destroy_all undo - ruby

I've accidentally erased all the records from one my model.
Model.destroy_all
For the output I've received a large list for all the records that have been destroyed.
=> [#<Model id: 1, some_attribute: "Hello World">, #<Model id: 2, some_attribute: " Hello World 2">, etc etc etc]
But, I've got it a text.
Can I do anything, using IRB, to return the records back ?
This is very, VERY urgent! Any help is appreciated.
Thank you so much

The following script should do the trick:
require 'bigdecimal'
str = "#<Model id: 1, some_attribute: #<BigDecimal:4ba0730,'0.0',9(18)>, another_attribute: \"Hello World\">, #<Model id: 2, some_attribute: \" Hello World 2\">"
str.scan(/#?<(\w+) (.+?)>(?=, #|$)/) do |m|
model = Object.const_get(m[0])
m[1].gsub!(/#<BigDecimal:.+?('.+?').+?>/, "BigDecimal.new(\\1)")
eval("model.create(#{m[1]})")
end
This also handles instances of BigDecimal. In case you need to handle other special types you can just add another call to gsub!.

here's a quick test I made on my model:
1.
pry(main)> output = JobUser.first(10).to_s
=> "[#<JobUser id: 10001, instagram_user_id: 297705889, job_id: 2, invited: true, created_at: \"2013-09-23 21:53:37\", updated_at: \"2013-09-23 21:53:37\">, #<JobUser id: 10002, instagram_user_id: 36823356, job_id: 2, invited: true, created_at: \"2013-09-23 21:53:37\", updated_at: \"2013-09-23 21:53:37\">, #<JobUser id: 10003, instagram_user_id: 509682835, job_id: 2, invited: true, created_at: \"2013-09-23 21:53:37\", updated_at: \"2013-09-23 21:53:37\"> ....
2.
parsed = output.gsub('#<', '').gsub('>', '').gsub(/^\[/, '').gsub(/\]$/, '').split('JobUser').map(&:strip)
=>
"id: 10001, instagram_user_id: 297705889, job_id: 2, invited: true, created_at: \"2013-09-23 21:53:37\", updated_at: \"2013-09-23 21:53:37\",",
"id: 10002, instagram_user_id: 36823356, job_id: 2, invited: true, created_at: \"2013-09-23 21:53:37\", updated_at: \"2013-09-23 21:53:37\",",
"id: 10003, instagram_user_id: 509682835, job_id: 2, invited: true, created_at: \"2013-09-23 21:53:37\", updated_at: \"2013-09-23 21:53:37\"...
3.
parsed.shift because the first element in array will be a blank string
4.
records = parsed.map { |serialized_record| JobUser.new(eval "{ #{serialized_record} }") }
then you should probably run something like records.each { |record| record.save }
Please note that you should replace JobUser with your model name.
The point is you'll have to parse the string and insert it back into database
good luck!

Related

Laravel HasMany Relation is returning all models, not related ones

I'm using Laravel 5.5, I have a Movie model:
class Movie extends Model
{
public function comments(){
return $this->hasMany(Comment::class);
}
}
and a Comment model:
class Comment extends Model
{
public function movie(){
return $this->belongsTo(Movie::class);
}
}
I have a movie instance (stored in $movie variable):
id: "tt5726616",
title: "Call Me by Your Name",
original_title: "Call Me by Your Name",
rate: 4,
year: 2017,
length: "132",
language: "English, Italian, French, German",
country: "Italy, France, Brazil, USA",
director: "Luca Guadagnino",
created_at: "2018-01-21 15:28:31",
updated_at: "2018-01-21 15:28:31",
and I have 4 comments, 2 of them related to the corresponding movie:
all: [
App\Comment {#788
id: 1,
movie_id: "tt3967856",
author: "user1",
comment: "cool!",
rate: 2,
created_at: "2018-01-21 15:28:32",
updated_at: "2018-01-21 15:28:32",
},
App\Comment {#786
id: 2,
movie_id: "tt3967856",
author: "user2",
comment: "not bad!",
rate: 3,
created_at: "2018-01-21 15:28:32",
updated_at: "2018-01-21 15:28:32",
},
App\Comment {#785
id: 3,
movie_id: "tt5726616",
author: "user1",
comment: "cool!",
rate: 4,
created_at: "2018-01-21 15:28:32",
updated_at: "2018-01-21 15:28:32",
},
App\Comment {#784
id: 4,
movie_id: "tt5726616",
author: "user2",
comment: "not bad!",
rate: 5,
created_at: "2018-01-21 15:28:32",
updated_at: "2018-01-21 15:28:32",
},
],
the problem is, when I call $movie->comments, it returns all my 4 comments, not just those two with movie_id tt3967856. What should I do?
SOLVED:
I think that was because I was using string type for my primary and foreign keys. I changed ids to integer (I mean 1,2,... instead of "tt3967856" etc.) and everything worked fine :D
What if you specify foreign and local keys
class Movie extends Model
{
protected $casts=['movie_id'=>'integer'];
public function comments(){
return $this->hasMany(Comment::class,movie_id,id);
}
}
update:
I also noticed that movie_id has string type in comment object
You sbould cast it to integer

What is inside my ruby array?

So I have an Array filled of what exactly?
myArray = [ #<Follower id: 1, username: "Prep Bootstrap", imageurl: "http://pbs.twimg.com//profile_images/2825468445/2a4...", user_id: "thefonso", follower_id: "2397558816", created_at: "2014-05-21 15:29:03", updated_at: "2014-05-21 15:29:03">, #<Follower id: 2, username: "JAVA Developer", imageurl: "http://pbs.twimg.com//profile_images/2825468445/2a4...", user_id: "thefonso", follower_id: "2352382640", created_at: "2014-05-21 15:29:05", updated_at: "2014-05-21 15:29:05">,
Follower id: 3, username: "JAVA Developer two", imageurl: "http://pbs.twimg.com//profile_images/2825468445/2a4...", user_id: "thefonso", follower_id: "2352382641", created_at: "2014-05-21 17:29:05", updated_at: "2014-05-21 17:29:05"> ]
Now I have many more of these inside this array with consecutive ids, etc..I'm confused by the #< >, is this an array of hashes? What am I looking at exactly? What is this an array of?
It looks like ActiveRecord's Followers class instances. Plus, it looks like you named your model improperly (with Rails practices, it should be named Follower).
Get the class of one of the objects and you'll know it
puts myArray[0].class

Arrange unique contents of hash according to there occurences

Suppose I am firing a activerecord statement and the resulting array of hash contains the following
I am sorry for the vague question without providing proper information
to be precise after running Item.joins(:item_categories).where(:item_categories => {:category_id => i.id})
I get the following
[#<Item id: 1, name: "Bat", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 1, name: "Bat", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 1, name: "Bat", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 2, name: "Base", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 2, name: "Base", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 2, name: "Base", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 3, name: "Glove", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 3, name: "Glove", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 3, name: "Glove", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 4, name: "Ball", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 4, name: "Ball", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 4, name: "Ball", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 5, name: "Catchers Mitt", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 5, name: "Catchers Mitt", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 5, name: "Catchers Mitt", created_at: "2014-02-10 00:28:43", updated_at: "2014-02-10 00:28:43">,
#<Item id: 6, name: "Batting Glove", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 6, name: "Batting Glove", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 6, name: "Batting Glove", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 7, name: "Batting Helmet", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 7, name: "Batting Helmet", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 7, name: "Batting Helmet", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 8, name: "Baseball Cap", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 8, name: "Baseball Cap", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 8, name: "Baseball Cap", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 9, name: "Gear Cycle", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 9, name: "Gear Cycle", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 10, name: "Non Gear Cycle", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 10, name: "Non Gear Cycle", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 11, name: "Mountain Bike", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 11, name: "Mountain Bike", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 12, name: "Uni-cycle", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">,
#<Item id: 12, name: "Uni-cycle", created_at: "2014-02-10 00:28:44", updated_at: "2014-02-10 00:28:44">]
In this case I not only want the output to be unique but also the hashes should be arranged according to the occurrences of there Item id:
Is there any method available for this?, do I have to work on my activerecord query or do I have to write the method to do this ?
Assuming you meant to show an array of hashes and not hashes with duplicate keys, you can use Array#uniq with a block to specify the criteria for uniqueness:
array = [
{ a: 1, b: 2 },
{ a: 1, b: 3 },
{ a: 2, b: 1 },
{ a: 2, c: 4 }
]
array.uniq {|hash| hash[:a] }
#=> [{ a: 1, b: 2 }, { a: 2, b: 1 }]
Hashes can't have duplicate keys, so no this is not possible. See the documentation at
http://www.ruby-doc.org/core-2.1.0/Hash.html
"A Hash is a dictionary-like collection of unique keys and their values."
If you executed this:
h = {'name' => 'hank', 'name' => 'hank','name' => 'steve',
'name' => 'brad', 'name' => 'brad','name' => 'brad' }
Ruby would not complain. It would set h['name'] to 'hank', then it would do that again, then it would change h['name'] to 'steve' and then set h['name'] to 'brad' three times, so you would end up with
h = { 'name' => 'brad' }
If you changed the keys so they were unique, wanted each value to appear only once and didn't care which key/value pairs you removed to accomplish that, here's an easy way to do it:
h = {'name1' => 'hank', 'name2' => 'hank', 'name3' => 'steve',
'name4' => 'brad', 'name5' => 'brad', 'name6' => 'brad' }
h.invert.invert #=> {"name2"=>"hank", "name3"=>"steve", "name6"=>"brad"}

Split array. Ruby

Could you help me please with some problem?
I have array from database like this
str = Hiring::Tag.all
Hiring::Tag Load (0.1ms) SELECT `hiring_tags`.* FROM `hiring_tags`
=> [#<Hiring::Tag id: 1, name: "tag1", created_at: "2013-12-10 11:44:39", updated_at: "2013-12-10 11:44:39">,
#<Hiring::Tag id: 2, name: "tag2", created_at: nil, updated_at: nil>,
#<Hiring::Tag id: 3, name: "tag3", created_at: nil, updated_at: nil>,
#<Hiring::Tag id: 4, name: "wtf", created_at: "2013-12-11 07:53:04", updated_at: "2013-12-11 07:53:04">,
#<Hiring::Tag id: 5, name: "new_tag", created_at: "2013-12-11 10:35:48", updated_at: "2013-12-11 10:35:48">]
And I need to split this array like this:
data:[{id:1,name:'tag1'},{id:2,name:'tag2'},
{id:3,name:'tag3'},{id:4,name:'wtf'},{id:5,name:'new_tag'}]
Help me please!
if you use ActiveRecord 4.0
Hiring::Tag.pluck(:id, :name).map{ |id, name| {id: id, name: name} }
One possible solution:
Hiring::Tag.all.map {|h| {id: h.id, name: h.name} }
See the documentation for map.
You can try below code.
Hiring::Tag.all.inject({}) { |h, f| h[f.name.to_sym] = f.value; h }
OR
Hiring::Tag.all.map { |f| [f.name.to_sym, f.value] }]

How i can find uniq records on base of some attribute in sort_by rails

Hi i have these records how i can find unique records in sort_by methods block
my array is like this
[#<Post id: 1, name: "ali", team_elo: 0, team_name: "solo", created_at:
"2012-12-09 16:11:17", updated_at: "2012-12-11 03:39:43">,
#<Post id: 2, name: "ramiz", team_elo: nil, team_name: "roko", created_at:
"2012-12-11 03:40:31", updated_at: "2012-12-11 03:40:31">,
#<Post id: 3, name: "ramizrt", team_elo: nil, team_name: "joko", created_at:
"2012-12-11 03:40:47", updated_at: "2012-12-11 03:40:47">,
#<Post id: 4, name: "lee", team_elo: nil, team_name: "roko", created_at:
"2012-12-11 03:41:15", updated_at: "2012-12-11 03:41:15">]
how i can sort this array on base of unique team_name.The result should like this
[#<Post id: 1, name: "ali", team_elo: 0, team_name: "solo", created_at:
"2012-12-09 16:11:17", updated_at: "2012-12-11 03:39:43">,
#<Post id: 2, name: "ramiz", team_elo: nil, team_name: "roko", created_at:
"2012-12-11 03:40:31", updated_at: "2012-12-11 03:40:31">,
#<Post id: 3, name: "ramizrt", team_elo: nil, team_name: "joko", created_at:
"2012-12-11 03:40:47", updated_at: "2012-12-11 03:40:47">]
Thanks
You could do something like:
arr = arr.uniq { |x| x.team_name }
or its brother
arr.uniq! { |x| x.team_name }
which will set arr to only the elements you want.

Resources