I have imported CSV file in Neo4j database. Part of code looks like this:
LOAD CSV WITH HEADERS from "file:///AccountTry.csv" as row
WITH row, split(row.birth_date, '-') as date
CREATE (a:AccountTest {id: toInteger(row.id), account_type: row.account_type, first_name: row.first_name, last_name: row.last_name })
and that works fine. I can see all my nodes and their attributes in neo4j browser.
Then, i created model in rails application:
class AccountTest
include Neo4j::ActiveNode
property :first_name, type: String
property :id, type: Integer
property :last_name, type: String
property :account_type, type: String
end
I made migration and that works fine. When i open rails console and try "AccountTest.first" i get all atributes fine, only property "AccountTest.id: nil".
Why is "id" nil? I have 6 nodes and all of them in rails application have id = nil, but in Neo4j browser all of them have correct ids.
I don't think you need to declare the id property in the AccountTest class. This property is the primary key and is there by default.
As this is an additional property (different form the id generated by Neo4j) I suggest that you rename the property on AccountTest (e.g. account_id) and change the CSV import to load the value into the account_id field.
Related
I am using mongoid on a rails project.
I have an API call that fetches client records in JSON format (array of hashes).
users = api.get_users # Returns JSON
To leverage Mongo's search, sort, and pagination, I'd like to store the records I get through API in the database.
Of course I could run over every record in the JSON and do something like User.create(user), but I would like to just import all records at once and create each record in the database. Perhaps using https://docs.mongodb.com/manual/reference/program/mongoimport ?
Any suggestions?
You're right -- because your data is in JSON format, mongoimport is the tool you want to use. Once you've imported your data, you can set up Mongoid document schemas to match the data you've imported.
Here's a helpful mongoimport tutorial if you want to try it on some sample data.
For the inventory data in this tutorial, you could set up the schema:
# JSON data: { "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" }
class Inventory
include Mongoid::Document
store_in collection: 'inventory'
field :item, type: String
field :qty, type: Integer
field :size, type: Hash
field :status, type: String
end
Some things to note here:
Make sure that you import your data to the default collection name for the model you want to create (i.e. import to the "users" collection for a model called User) OR indicate on the model which collection the data is stored in (using the store_in method)
When you import data as a Hash, you have some options on how to represent that in Mongoid. I've just used a Hash in this example, but you could also make size its own embedded document.
I want to delete a field in a document using ROR.
I have already tried
book.remove_attribute(:name)
book.unset(:name)
But they both set the attribute to nil and it is still present in the object.
I want it to vanish from my document. Any help is welcome.
When you access a document via mongoid, it returns you a Ruby object. You can actually see the data stored in the document only via mongo shell (just type 'mongo' in you terminal).
The object is created by Mongoid (MongoDB ODM/wrapper for rails). This object may occasionally look different from the document.
For example
When you unset a field, that field is entirely removed from that document. BUT, since your model still has that field on it, MONGOID returns you a nil attribute for that field, instead of giving you different number of fields for objects of same model.
Model book.rb
class Book
include Mongoid::Document
field :name
field :author
end
In rails console, type
Book.create(name: "b1", author: "a1")
=> #<Book _id: 555231746c617a1c99030000, name: "b1", author: "a1">
In Mongo shell
db.books.find()
{ "_id" : ObjectId("555231746c617a1c99030000"), "name" : "b1", "author" : "a1" }
Now, we unset.
In rails console
Book.first.unset(:name)
=> #<Book _id: 555231746c617a1c99030000, name: nil, author: "a1">
In Mongo shell
db.books.find()
{ "_id" : ObjectId("555231746c617a1c99030000"), "author" : "a1" }
If however you still dont want to see the field in your rails console (mind you, this is not taking up any extra space in db) you can always remove the field from the model. If you do that, you will no longer be able to access this field through rails/mongoid on any object. It will only be present on the document and accessible through mongo shell.
Given the following pseudo-cql table structure:
CREATE TABLE searches (
category text,
timestamp timestamp,
no_of_searches int,
avg_searches double,
PRIMARY KEY((category, timestamp), no_of_searches)
);
and the following Rails Cequel model:
class Search
include Cequel::Record
# Table columns
key :category, :text
key :timestamp, :timestamp
key :no_of_searches, :int
column :avg_searches, :double
end
when I try to synchronise the model using:
rake cequel:migrate
the following rake error is thrown:
rake aborted!
Cequel::InvalidSchemaMigration: Existing partition keys category,timestamp differ from specified partition keys timestamp
I'm trying to get the above rails model to synchronise with the above table using the partition keys, although it's stating that the two sets of keys are different. I've tried defining the keys in the same order, but has not worked.
My objective is to get the pre-defined database table with partition keys working with the rails model. Any help would be grateful!
The key method supports an options hash as a third parameter. The options hash adds further support to the defined key such as order and partitioning.
Based upon the given table definition it would mean your table columns would look something like this:
# Table columns
key :category, :text, { partition: true }
key :timestamp, :timestamp, { partition: true }
key :no_of_searches, :int
column :avg_searches, :double
Unfortunately this is not documented within the Cequel README, however it is documented within the code, for which can be found here.
I have a model in a Rails 4.0.3 app which uses Mongoid 4 (master branch directly from GitHub), and I'm trying to ensure an index on multiple fields to be unique and to drop duplicates.
class MyModel
include Mongoid::Document
field :a, type: Integer
field :b, type: Integer
index({a: 1, b: 1}, {unique: true, dropDups: true, name: 'unique_drop_dups_idx'})
But when I run the command to create indexes:
rake db:mongoid:create_indexes
I get this error:
Problem:
Invalid index specification on MyModel: {:a=>1, :b=>1}, {:unique=>true, :dropDups=>true, :name=>"unique_drop_dups_idx"}
Summary:
Indexes in Mongoid are defined as a hash of field name and direction/2d pairs, with a hash for any additional options.
Resolution:
Ensure that the index conforms to the correct syntax and has the correct options.
The index creation starts if I get rid of the dropDups option, even if it fails eventually because of the presence of duplicates.
Does the error message means that is not possible to create an index on multiple fields with this configuration (unique + dropDups)? Am I missing something else?
dropDups is not a valid index option for mongoid. You need to use drop_dups instead.
index({a: 1, b: 1}, {unique: true, drop_dups: true, name: 'unique_drop_dups_idx'})
We try not to use camel case for options as we are on ruby land. You can see the mappings in here https://github.com/mongoid/mongoid/blob/master/lib/mongoid/indexable/specification.rb#L14.
Hope that helps.
I would like to know if it is possible to get the types (as known by AR - eg in the migration script and database) programmatically (I know the data exists in there somewhere).
For example, I can deal with all the attribute names:
ar.attribute_names.each { |name| puts name }
.attributes just returns a mapping of the names to their current values (eg no type info if the field isn't set).
Some places I have seen it with the type information:
in script/console, type the name of an AR entity:
>> Driver
=> Driver(id: integer, name: string, created_at: datetime, updated_at: datetime)
So clearly it knows the types. Also, there is .column_for_attribute, which takes an attr name and returns a column object - which has the type buried in the underlying database column object, but it doesn't appear to be a clean way to get it.
I would also be interested in if there is a way that is friendly for the new "ActiveModel" that is coming (rails3) and is decoupled from database specifics (but perhaps type info will not be part of it, I can't seem to find out if it is).
Thanks.
In Rails 3, for your model "Driver", you want Driver.columns_hash.
Driver.columns_hash["name"].type #returns :string
If you want to iterate through them, you'd do something like this:
Driver.columns_hash.each {|k,v| puts "#{k} => #{v.type}"}
which will output the following:
id => integer
name => string
created_at => datetime
updated_at => datetime
In Rails 5, you can do this independently of the Database. That's important if you use the new Attributes API to define (additional) attributes.
Getting all attributes from a model class:
pry> User.attribute_names
=> ["id",
"firstname",
"lastname",
"created_at",
"updated_at",
"email",...
Getting the type:
pry> User.type_for_attribute('email')
=> #<ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlString:0x007ffbab107698
#limit=255,
#precision=nil,
#scale=nil>
That's sometimes more information than needed. There's a convenience function that maps all these types down to a core set (:integer, :string etc.)
> User.type_for_attribute('email').type
=> :string
You can also get all that data in one call with attribute_types which returns a 'name': type hash.
You can access the types of the columns by doing this:
#script/console
Driver.columns.each {|c| puts c.type}
If you want to get a list of all column types in a particular Model, you could do:
Driver.columns.map(&:type) #gets them all
Driver.columns.map(&:type).uniq #gets the unique ones
In rails 5 this will give you a list of all field names along with their data type:
Model_Name.attribute_names.each do |k| puts "#{k} = #{Model_Name.type_for_attribute(k).type}" end
Rails 5+ (works with virtual attributes as well):
Model.attribute_types['some_attribute'].type
This snippet will give you all the attributes of a model with the associated database data types in a hash. Just replace Post with your Active Record Model.
Post.attribute_names.map {|n| [n.to_sym,Post.type_for_attribute(n).type]}.to_h
Will return a hash like this.
=> {:id=>:integer, :title=>:string, :body=>:text, :created_at=>:datetime, :updated_at=>:datetime, :topic_id=>:integer, :user_id=>:integer}
Assuming Foobar is your Active Record model. You can also do:
attributes = Foobar.attribute_names.each_with_object({}) do |attribute_name, hash|
hash[attribute_name.to_sym] = Foobar.type_for_attribute(attribute_name).type
end
Works on Rails 4 too
In Rails 4 You would use Model.column_types.