Record Persistence in Neo4j.rb 8.0.x - ruby-on-rails

I am in the process of upgrading to Neo4j.rb version 8. After following the instructions in the upgrade guide, specs that were passing before are now failing.
Specifically, count, persisted?, all, last and find seem to have come unhinged from each other. I can create an account, see that is is persisted, see it if I count accounts, but not if I look for the last account, or all accounts, or try to find it from its id.
Output from console session below. Am I crazy?
[1] pry> account = Account.create(company_name: "Acme Corporation, LLC", street_address: "1234 Fake Lane", city: "Pleasantville", state: "CA", zip: "12345", country: "United States", phone: "555-555-5555", contact_name: "Some Guy", contact_phone: "123-456-7890", contact_email: "someguy#acmecorp.com")
=> #<Account uuid: "30c44118-ac45-4559-b63b-8e82fafb16cd", city: "Pleasantville", company_name: "Acme Corporation, LLC", contact_email: "someguy#acmecorp.com", contact_name: "Some Guy", contact_phone: "123-456-7890", country: "United States", created_at: Thu, 23 Mar 2017 19:13:52 +0000, phone: "555-555-5555", remote_id: nil, state: "CA", street_address: "1234 Fake Lane", updated_at: Thu, 23 Mar 2017 19:13:52 +0000, zip: "12345">
[2] pry> account.persisted?
=> true
[3] pry> Account.count
=> 1
[4] pry> Account.last
=> nil
[5] pry> Account.find(account.id)
Neo4j::ActiveNode::Labels::RecordNotFound: Couldn't find Account with 'uuid'=30c44118-ac45-4559-b63b-8e82fafb16cd

And the answer is....... RESTART YOUR COMPUTER AND EVERYTHING WORKS!!!111
In reality the problem was that somewhere along the line I deleted the database rather than stopping it, and then installed a new copy.

Related

Rails showing all data from database to index page how to stop it?

i am new in rails and i have generated a resource named a company name:string bio:text and ceo:string but my index view showing all the db records but i haven't done any inspet so why it showing all the data in index page?
it showing like this
[#<Company id: 4, name: "google", bio: "ad company", ceo: "sundar pichaif", created_at: "2021-03-15 12:43:18.821528000 +0000", updated_at: "2021-03-15 13:05:49.407986000 +0000">, #<Company id: 5, name: "basecamp", bio: "task management", ceo: "jason fried", created_at: "2021-03-15 13:27:58.781628000 +0000", updated_at: "2021-03-15 13:27:58.781628000 +0000">, #<Company id: 6, name: "Github", bio: "code", ceo: "Chris Wanstrath", created_at: "2021-03-15 13:30:15.510656000 +0000", updated_at: "2021-03-15 13:30:55.654182000 +0000">]
please help me out :(
open your index file where it shows the data from your database, please remove "=" for example: remove this <%= #companies %> and add like this <% #companies %>, the reason I am telling you to remove is that when you use "=" this sign with <%= ..... %>, it will show all the data in this erb brackets, so remove this and your issue will be resolve, Thanks

is it possible to override built-in Ruby methods?

I am working on a problem where I have to pass an rpsec test. The problem is that the method is using the same name as a built in ruby method .count
given that I cannot change the rspec test, is it possible to override .count to behave differently? if not, is there a better way to get around this?
here is the rspec test I am trying to pass
subject = FinancialSummary.one_day(user: user, currency: :usd)
expect(subject.count(:deposit)).to eq(2)
my code:
class FinancialSummary
def self.one_day(user: user, currency: currency)
one_day_range = Date.today.beginning_of_day..Date.today.end_of_day
find_transaction(user.id, currency).where(created_at: one_day_range)
end
def self.find_transaction(user_id, currency)
Transaction.where(user_id: user_id,
amount_currency: currency.to_s.upcase
)
end
end
output:
[#<Transaction:0x00007f9b39c2e9b8
id: 1,
user_id: 1,
amount_cents: 1,
amount_currency: "USD",
category: "deposit",
created_at: Sat, 10 Mar 2018 18:46:53 UTC +00:00,
updated_at: Sat, 10 Mar 2018 18:46:53 UTC +00:00>,
#<Transaction:0x00007f9b3d0dbc38
id: 2,
user_id: 1,
amount_cents: 2000,
amount_currency: "USD",
category: "deposit",
created_at: Sat, 10 Mar 2018 18:47:43 UTC +00:00,
updated_at: Sat, 10 Mar 2018 18:47:43 UTC +00:00>,
#<Transaction:0x00007f9b3d0b3fa8
id: 7,
user_id: 1,
amount_cents: 1200,
amount_currency: "USD",
category: "withdraw",
created_at: Mon, 05 Mar 2018 02:22:42 UTC +00:00,
updated_at: Tue, 06 Mar 2018 18:48:20 UTC +00:00>]
it is printing out, what I believe to be the correct information, up until the test attempts to count the transactions by their category: 'deposit'. Then I get this error message:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: deposit: SELECT COUNT(deposit) FROM "transactions" WHERE "transactions"."user_id" = ? AND "transactions"."amount_currency" = ?
EDITED FOR MORE INFO
Some Assumptions Were Made in the Writing of this answer and modifications may be made based on updated specifications
Overriding count is a bad idea because others who view or use your code will have no idea that this is not the count they know and understand.
Instead consider creating a scope for this like
class FinancialSummary < ApplicationRecord
scope :one_day, ->(user:,currency:) { where(user: user, currency: currency) } #clearly already a scope
scope :transaction_type, ->(transaction_type:) { where(category: transaction_type) }
end
then the test becomes
subject = FinancialSummary.one_day(user: user, currency: :usd)
expect(subject.transaction_type(:deposit).count).to eq(2)
SQL now becomes:
SELECT COUNT(*)
FROM
"transactions"
WHERE
"transactions"."user_id" = ?
AND "transactions"."amount_currency" = "usd"
AND "transactions"."category" = "deposit"
Still very understandable and easy to read without the need to destroy the count method we clearly just used.
It's not clear what object the count message is being sent to because I don't know what FinancialSummary.one_day(user: user, currency: :usd) returns, but it seems like you are saying count is a method on whatever it returns, that you can't change. What does FinancialSummary.one_day(user: user, currency: :usd).class return?
Perhaps one solution would be to alias it on that object by adding alias_method :count, :account_count and then in your test calling expect(subject.account_count(:deposit)).to eq(2)
It would be easier if you could post the FinancialSummary#one_day method in your question.

Some values are nil (which were set) when parsing JSON in Ruby using the "json" gem

when I parse the following json in my rails console then the contact_person_id is nil even it was "c221c0f96670db455a174f1f30ffef1a". I am using the normal "json" gem. I tried to use some other library for example "yajl_ruby" but then something other broke. has anybody an idea why this happens?
ActiveSupport::JSON.decode '{"_id":"a042b081278fc535f50fd3f4ea695848","_rev":"7-435d6ef891d2d354a7233674c483194b","created_at":"2011-12-12T18:39:19Z","updated_at":"2011-12-12T22:34:35Z","contact_person_id":"c221c0f96670db455a174f1f30ffef1a","first_person_in_authority_id":null,"second_person_in_authority_id":null,"name":"","street":"","postcode":"","city":"","ruby_class":"Community"}'
=> #<Community _id: "a042b081278fc535f50fd3f4ea695848", _rev: "7-435d6ef891d2d354a7233674c483194b", created_at: Mon, 12 Dec 2011 18:39:19 UTC +00:00, updated_at: Mon, 12 Dec 2011 22:34:35 UTC +00:00, contact_person_id: nil, first_person_in_authority_id: nil, second_person_in_authority_id: nil, name: "", street: "", postcode: "", city: "", regional_chirch: nil, deanery: nil, chirch_district: nil, state: nil, urban_district: nil, county: nil, administrative_district: nil>
I cannot confirm this problem with the following versions:
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.0.0]
activesupport (3.1.3)
I used the following snippet:
irb(main):003:0> ActiveSupport::JSON.decode '{"_id":"a042b081278fc535f50fd3f4ea695848","_rev":"7-435d6ef891d2d354a7233674c483194b","created_at":"2011-12-12T18:39:19Z","updated_at":"2011-12-12T22:34:35Z","contact_person_id":"c221c0f96670db455a174f1f30ffef1a","first_person_in_authority_id":null,"second_person_in_authority_id":null,"name":"","street":"","postcode":"","city":"","ruby_class":"Community"}'
=> {"city"=>"", "name"=>"", "created_at"=>"2011-12-12T18:39:19Z", "postcode"=>"", "first_person_in_authority_id"=>nil, "_rev"=>"7-435d6ef891d2d354a7233674c483194b", "updated_at"=>"2011-12-12T22:34:35Z", "_id"=>"a042b081278fc535f50fd3f4ea695848", "street"=>"", "contact_person_id"=>"c221c0f96670db455a174f1f30ffef1a", "second_person_in_authority_id"=>nil, "ruby_class"=>"Community"}
irb(main):004:0> _['contact_person_id']
=> "c221c0f96670db455a174f1f30ffef1a"
I think the problem lies within your codebase i.e. the Community model.
Things to check:
Is there some after_initialization hook?
Is there a custom contact_person_id= setter?
Is contact_person a relation that must exist with the id c221c0f96670db455a174f1f30ffef1a?
Without any further information, I'm sorry that I cannot give you a better answer.

Access just the value of BSON::ObjectId in ruby

I'm working on a ruby/rails app backed by Mongodb (using Mongoid). Within the context of the Rails application everything works flawlessly but we're also accessing objects outside of the Rails environment, where I'm having trouble getting the id of an object to return as anything but a hash in the format:
{"$oid"=>"4e0005b78ba4db213500001f"}
I've figured out that I'm seeing because I'm getting back a value that's not just an id string but rather of the type BSON::ObjectId. In addition to requiring the rails environment I've also tried requiring bson explicitly in the file that's doing this work:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bson"
I need to get the value simply as a string containing the id, which seems to be the default everywhere else but in this one case. Oddly this behavior only occurs in my dev environment (the rest of the guys on my team don't have this problem).
Requisite disclaimer that I'm new to Mongodb so could be missing something truly obvious.
Thanks!
You could try calling to_s on the object. In irb:
ruby-1.9.2-p180 > p = Project.last #=> #<Project _id: 4e00e77d399a46759d000002, _type: nil, version: 1, created_at: 2011-06-21 18:48:34 UTC, updated_at: 2011-06-21 18:48:34 UTC, name: "Testing MongoDB", client_id: 3, client_name: nil, group_id: 35, requestor_id: 14, requestor_name: "Test Client User", requestor_phone: "", creator_id: 2, creator_name: "Some Guy", manager_id: 23, manager_name: "Some Other Guy", manager_phone: "", manager_email: "", active: true, status: "open", default_hourly_cost: "0.0", default_hourly_charge: "0.0", default_material_markup: "0.35", add_email_internal: "", add_email_client: "", client_po_number: "", client_ticket_number: "", date_requested: nil, date_requested(1i): "2011", date_requested(2i): "6", date_requested(3i): "21">
ruby-1.9.2-p180 > p.id.to_s #=> "4e00e77d399a46759d000002"
If that doesn't work, can you post your environment.rb?

How to get nice formatting in the Rails console

I want to get something like this to look nice:
>> ProductColor.all
=> [#<ProductColor id: 1, name: "White", internal_name: "White", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 2, name: "Ivory", internal_name: "Ivory", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 3, name: "Blue", internal_name: "Light Blue", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 4, name: "Green", internal_name: "Green", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">]
This doesn't work:
>> ProductColor.all.inspect
=> "[#<ProductColor id: 1, name: \"White\", internal_name: \"White\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 2, name: \"Ivory\", internal_name: \"Ivory\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 3, name: \"Blue\", internal_name: \"Light Blue\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 4, name: \"Green\", internal_name: \"Green\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">]"
And neither does this:
>> ProductColor.all.to_yaml
=> "--- \n- !ruby/object:ProductColor \n attributes: \n name: White\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"1\"\n internal_name: White\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Ivory\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"2\"\n internal_name: Ivory\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Blue\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"3\"\n internal_name: Light Blue\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Green\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"4\"\n internal_name: Green\n attributes_cache: {}\n\n"
Thoughts?
The y method is a handy way to get some pretty YAML output.
y ProductColor.all
Assuming you are in script/console
As jordanpg commented, this answer is outdated. For Rails 3.2+ you need to execute the following code before you can get the y method to work:
YAML::ENGINE.yamler = 'syck'
From ruby-docs
In older Ruby versions, ie. <= 1.9, Syck is still provided, however it
was completely removed with the release of Ruby 2.0.0.
For rails 4/ruby 2 you could use just
puts object.to_yaml
You should try hirb. It's a gem made to to pretty format objects in the ruby console. Your script/console session would look like this:
>> require 'hirb'
=> true
>> Hirb.enable
=> true
>> ProductColor.first
+----+-------+---------------+---------------------+---------------------+
| id | name | internal_name | created_at | updated_at |
+----+-------+---------------+---------------------+---------------------+
| 1 | White | White | 2009-06-10 04:02:44 | 2009-06-10 04:02:44 |
+----+-------+---------------+---------------------+---------------------+
1 row in set
=> true
You can learn more about hirb at its homepage.
Awesome print is nice too if you want an object indented. Something like:
$ rails console
rails> require "awesome_print"
rails> ap Account.all(:limit => 2)
[
[0] #<Account:0x1033220b8> {
:id => 1,
:user_id => 5,
:assigned_to => 7,
:name => "Hayes-DuBuque",
:access => "Public",
:website => "http://www.hayesdubuque.com",
:toll_free_phone => "1-800-932-6571",
:phone => "(111)549-5002",
:fax => "(349)415-2266",
:deleted_at => nil,
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
:email => "info#hayesdubuque.com",
:background_info => nil
},
[1] #<Account:0x103321ff0> {
:id => 2,
:user_id => 4,
:assigned_to => 4,
:name => "Ziemann-Streich",
:access => "Public",
:website => "http://www.ziemannstreich.com",
:toll_free_phone => "1-800-871-0619",
:phone => "(042)056-1534",
:fax => "(106)017-8792",
:deleted_at => nil,
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
:email => "info#ziemannstreich.com",
:background_info => nil
}
]
To integrate it by default with your irb/rails/pry console, add to your ~/.irbrc or ~/.pryrc file:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
>> puts ProductColor.all.to_yaml
Simply works fine!
Source: https://stackoverflow.com/a/4830096
May also be noted that you can use:
j ProductColor.all.inspect
to output in Json format rather than Yaml
I think this solution is the most accurate one. You should try this:
puts JSON.pretty_generate Entry.all.map(&:attributes)
This will give you a super nice output compare to YAML format:
[
{
"id": 44,
"team_id": null,
"member_id": 1000000,
"match_id": 1,
"created_at": "2019-04-09 15:53:14 +0900",
"updated_at": "2019-04-09 15:53:14 +0900"
},
{
"id": 45,
"team_id": null,
"member_id": 1000001,
"match_id": 1,
"created_at": "2019-04-09 15:53:36 +0900",
"updated_at": "2019-04-09 15:53:36 +0900"
},
{
"id": 46,
"team_id": null,
"member_id": 1000003,
"match_id": 1,
"created_at": "2019-04-09 15:56:40 +0900",
"updated_at": "2019-04-09 15:56:40 +0900"
},
{
"id": 47,
"team_id": null,
"member_id": 1000004,
"match_id": 1,
"created_at": "2019-04-09 15:56:48 +0900",
"updated_at": "2019-04-09 15:56:48 +0900"
}
]
Hi you can also try this in your script/console if
>> y ProductColor.all
not working for you.
Try this:
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
then
>> y ProductColor.all
I had some troubles making it work so I'll add my two cents to awesome_print
add this to your Gemfile, preferably in :development
gem 'awesome_print', require: 'ap'
then in
rails console
you can do
> ap Model.all
That's it. However you can also add
require "awesome_print"
AwesomePrint.irb!
to your ~/.irbrc, this way awesome_print will be required anytime you open the console and you can simply do
Model.all
without the need of typing ap
You may also try the following for a group of objects
Object.all.map(&:attributes).to_yaml
This will give you much nicer output, like
---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California
Calling to_yaml on attributes rather than the object itself saves you from viewing the full contents of the object in the output
Or puts Object.last.attributes.to_yaml for a single object
Shorthand is also available: y Object.last.attributes
Use irbtools gem.
It will automatically format the the console output plus you'll get tons of great features.
You might want to define ProductColor's inspect method to return something that you find nice. For example:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
After which the result of ProductColor.all will display as something like [<1 - White (White)>, ...]. Of course you should adjust the inspect method to your needs, so that it displays all the information you need in a style that you like.
Edit: also if the issue was the lack of line breaks in the output, you might try
require 'pp'
pp ProductColor.all
which should insert linebreaks where appropriate
To add to Alter Lago's suggestion for using AwesomePrint,
If you can't/shouldn't/don't want to add the awesome_print gem to your project's Gemfile, do this:
gem install awesome_print
Edit ~/.irb.rc and add this:
$LOAD_PATH << '/Users/your-user/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib'
require 'awesome_print'
(Making sure the path and version are correct, of course)

Resources