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] }]
Related
Lets say I have an array with several hashes of emails and names. For example I have something like this:
foo = [{id: 1, name: 'Eric Cartman', email: 'eric#southpark.com'},
{id: 2, name: 'Eric Cartman', email: 'cartmanfamily#gmail.com'},
{id: 3, name: "Cartman's mom", email: 'cartmanfamily#gmail.com'},
{id: 4, name: 'Eric Cartman', email: 'eric#southpark.com'}]
How can I use .uniq to return unique values based on the combination of name and email? For example I want to return something like this:
[{id: 1, name: 'Eric Cartman', email: 'eric#southpark.com'},
{id: 2, name: 'Eric Cartman', email: 'cartmanfamily#gmail.com'},
{id: 3, name: "Cartman's mom", email: 'cartmanfamily#gmail.com'}]
foo.uniq should work just fine.
Since
{name: "cartman", email: "cartman#sp.com"} == {name: "cartman", email: "cartman#sp.com"} # => True
{name: "stan", email: "stan#sp.com"} == {name: "cartman", email: "cartman#sp.com"} # => False
The == operator check if every field of the hash have the same values. So .uniq will work how you want it to work!
If there is more than only the email and name field you should use the uniq method with a block:
foo.uniq { |x| [x[:name], x[:email]] }
It will keep only the uniq combination of the name and email.
Hope it helped, happy ruby coding!
Array#uniq takes a block:
foo = [{id: 1, name: 'Eric Cartman', email: 'eric#southpark.com'},
{id: 2, name: 'Eric Cartman', email: 'cartmanfamily#gmail.com'},
{id: 3, name: "Cartman's mom", email: 'cartmanfamily#gmail.com'},
{id: 4, name: 'Eric Cartman', email: 'eric#southpark.com'}]
bar = foo.uniq {|h| [h[:name], h[:email]] }
bar == [{id: 1, name: 'Eric Cartman', email: 'eric#southpark.com'},
{id: 2, name: 'Eric Cartman', email: 'cartmanfamily#gmail.com'},
{id: 3, name: "Cartman's mom", email: 'cartmanfamily#gmail.com'}] #=> true
Per the documentation, "If a block is given, it will use the return value of the block for comparison."
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
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"}
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!
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.