Rails Neo4j Migration Argument Error - ruby-on-rails

So I have these two classes
class Tweet
include Neo4j::ActiveNode
property :content, type: String
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_one(:user, :tweeted).from(:User)
end
class User
include Neo4j::ActiveNode
property :username, type: String
property :name, type: String
property :email, type: String, default: ''
validates :email, :username, uniqueness: true
property :encrypted_password
has_many(:Tweet, :tweeted)
end
And everytime I run rails neo4j:migrate it gives error like this
ArgumentError: The 'type' option must be specified( even if it is nil) or origin/rel_class must be specified (Class#tweeted)
How to properly create relationship between nodes in neo4jrb?

As the error message describes, you didn't explicitly define the type of your tweeted relation. Take a look at the official documentation for further details. However, the following should work:
class Tweet
include Neo4j::ActiveNode
property :content, type: String
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_one :out, :author, type: :author, model_class: :User
end
class User
include Neo4j::ActiveNode
property :username, type: String
property :name, type: String
property :email, type: String, default: ''
validates :email, :username, uniqueness: true
property :encrypted_password
has_many :in, :tweets, origin: :author
end

Related

Why my app is taking this class as uninitialized constant Booking::String?

I am using mongodb through mongoid gem in rails. I am new to it. I've created a model in this which is following:
class Booking
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
embeds_many :extras
accepts_nested_attributes_for :extras
field :bedroom, type: Integer
field :bathroom, type: String
field :frequency, type: String
field :extras, type: String
field :zipcode, type: Integer
field :date, type: String
field :time, type: String
field :date_timestamp, type: Time
field :first_name, type: String
field :last_name, type: String
field :email, type: String
field :address, type: String
field :apartment, type: Strng
field :phone, type: String
field :alt_phone, type: String
field :key_info, type: Integer
field :key_details, type: String
field :coupon_code, type: String
field :gift_card_code, type: String
field :gift_card_amount, type: Float
field :referral_amount, type: Float
field :final_amount, type: Float
field :created_at, type: Time
field :updated_at, type: Time
end
And whenever I access it index page having code
class BookingsController < ApplicationController
def index
#bookings = Booking.all
end
end
It returns me uninitialized constant Booking::Strng error. I've spend hour in finding out its reason. Can anyone help me to resolve this.
Because String is spelled with an i :) See your :apartment line.
Btw, I'll add that this is so common in programming, never trust your eyes, all I did here was copied the string from the error and Cmd+F searched for it in this page (your code).

Ruby addtoset to a array field

When adding a value into a array field of a class, I am getting the error given below:
" Cannot apply $addToSet to a non-array field. Field named 'chat_user_ids' has a non-array type NULL in the document _id: ObjectId('5705fd637261695baa0f0000') ".
class Wall
include Mongoid::Document
include Mongoid::Timestamps::Created
include Mongoid::Timestamps::Updated
include Geo
include WallSearch
include Common
field :message, type: String
field :user_id, type: BSON::ObjectId
field :city, type: String
field :tag_id, type: BSON::ObjectId
field :group_id, type: BSON::ObjectId
field :tag_name, type: String
field :status, type: Boolean, default: true
field :abuse_count, type: Integer, default: 0
field :latitude, type: String
field :longitude, type: String
field :state, type: String
field :address, type: String
field :location, type: Array
field :chat_user_ids, type: Array
chat_user_ids is the array to which I am adding a value.
wall = Wall.where(_id: wall_id).first
return false unless wall.present?
wall.add_to_set(chat_user_ids: user_id)
user_id should rather be [user_id]
wall.add_to_set(chat_user_ids: [user_id])

How can I create unique relationships based on a model in neo4j.rb?

I have tried to use
has_many :in, :ratings, unique: true, rel_class: Rating
But that unique: true is ignored because I have a model class for the relationship.
How can I make sure that if my Users rate Articles, their rating gets updated instead of added. I'd prefer it if it produces a single query. ;-)
Article.rb:
class Article
include Neo4j::ActiveNode
property :title, type: String
property :body, type: String
property :created_at, type: DateTime
# property :created_on, type: Date
property :updated_at, type: DateTime
# property :updated_on, type: Date
has_many :in, :ratings, unique: true, rel_class: Rating
has_many :in, :comments, unique: true, type: :comment_on
has_one :in, :author, unique: true, type: :authored, model_class: User
end
User.rb:
class User
include Neo4j::ActiveNode
has_many :out, :articles, unique: true, type: :authored
has_many :out, :comments, unique: true, type: :authored
has_many :out, :ratings, unique: true, rel_class: Rating
# this is a devise model, so there are many properties coming up here.
Rating.rb
class Rating
include Neo4j::ActiveRel
property :value, type: Integer
from_class User
to_class :any
type 'rates'
property :created_at, type: DateTime
# property :created_on, type: Date
property :updated_at, type: DateTime
# property :updated_on, type: Date
end
Rating creation inside the article controller:
Rating.create(:value => params[:articleRating],
:from_node => current_user, :to_node => #article)
This has been resolved. You can ensure unique relationships while using an ActiveRel model by using the creates_unique keyword.
per https://stackoverflow.com/a/33153615
For now I found this ugly workaround..
def rate
params[:articleRating]
rel = current_user.rels(type: :rates, between: #article)
if rel.nil? or rel.first.nil?
Rating.create(:value => rating,
:from_node => current_user, :to_node => #article)
else
rel.first[:value] = rating
rel.first.save
end
render text: ''
end
EDIT: cleaner, but with two queries:
def rate
current_user.rels(type: :rates, between: #article).each{|rel| rel.destroy}
Rating.create(:value => params[:articleRating],
:from_node => current_user, :to_node => #article)
render text: ''
end

Query embedded document using slug array in Mongoid

I have 3 models, that all have use the gem - https://github.com/digitalplaywright/mongoid-slug - that creates a _slugs field from the models title field.
I have the slug for one of my articles and I need to find the issue it belongs to. I have tried a whole deal of things, but nothing seems to work.
Any advice on what the correct query is to get the issue that belongs to the article with my article slug?
Query that doesn't work:
p = Publication.find("my-publication")
p.issues.where(:'articles._slugs'.in => ["an-article-slug"]).first
Publication model:
class Publication
# 1. Include mongoid stuff
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
# 2. Define fields
field :title, type: String
field :description, type: String
field :published, type: Boolean, default: false
field :live, type: Boolean, default: false
field :show_walkthrough, type: Boolean, default: true
field :subscription_duration, type: String, default: "Subscription Duration"
field :subscription_price, type: String, default: "Price"
field :sell_issues_separately, type: String, default: "Individual Issue Sale"
field :issue_price, type: String, default: "Price"
field :previewed_on_device, type: Boolean, default: false
field :shareable, type: String, default: "Make Articles Shareable Online"
field :urban_airship_key, type: String
field :urban_airship_secret, type: String
field :urban_airship_master_secret, type: String
# 3. Set attributes accesible
attr_accessible :title, :description, :live, :published, :show_walkthrough, :subscription_duration, :subscription_price, :sell_issues_separately, :issue_price, :cover_image_attributes, :logo_image_attributes, :shareable, :urban_airship_key, :urban_airship_secret, :urban_airship_master_secret
# 4. Set slug
slug :title, reserve: ['new', 'edit', 'walkthrough', 'email', 'previewer', 'privacy', 'support', 'manifest', 'feed', 'demo', 'existence', 'switch']
# 5. Set associations
belongs_to :user
embeds_many :issues, order: :created_at.desc, cascade_callbacks: true
end
Issue model:
class Issue
# 1. Include mongoid stuff
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
# 2. Define fields
field :title, type: String
field :description, type: String
field :published, type: Boolean, default: false
field :last_push_at, type: DateTime, default: Time.now
field :published_at, type: DateTime, default: Time.now
field :no, type: Integer, default: 0
field :color, type: String
field :free, type: Boolean, default: false
# 3. Set attributes accesible
attr_accessible :title, :description, :published, :last_push_at, :published_at, :no, :color, :free, :cover_image_attributes
# 4. Set slug
slug :title, scope: :publication, reserve: ['new', 'edit', 'publish', 'update_order']
# 5. Set associations
embedded_in :publication
embeds_many :articles, :as => :articleable, :class_name => 'Article', cascade_callbacks: true, order: :no.desc
embeds_one :cover_image, :as => :imageable, :class_name => 'Image', cascade_callbacks: true, autobuild: true
end
Article model:
class Article
# 1. Include mongoid stuff
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
# 2. Define fields
field :title, type: String
field :author, type: String
field :lead, type: String
field :body, type: String
field :no, type: Integer
# 3. Set attributes accesible
attr_accessible :title, :author, :lead, :body, :no, :article_image_attributes, :article_images_attributes, :article_images_attributes
# 4. Set slug
slug :title, scope: :articleable
# 5. Set associations
embedded_in :articleable, polymorphic: true
embeds_one :article_image, :as => :imageable, :class_name => 'Image', cascade_callbacks: true, autobuild: true
embeds_many :article_images, :as => :imageable, :class_name => 'Image', cascade_callbacks: true
end
UPDATED With new queries and results
I can get the publication using the query suggestion of #mu below:
I can't get the issue using the same query though:
And here you can see the _slugs field if I take the first article in the first issue of the publication:
What am I doing wrong here? The query seems to work nicely when grabbing a publication. Why doesn't it work nicely when grabbing an issue?

Updating record with Mongoid in Rails

I am testing this from rails console:
Credential.last.token => nil
Credential.last.update_attribute :token, '123' => true
Credential.last.token => nil
Here is my class:
class Credential
include Mongoid::Document
include Mongoid::Timestamps
field :_id, type: String
field :user_id, type: Integer
field :code, type: String
field :provider, type: String
field :token, type: String
end
What am I doing wrong?
If you have identity map enabled you'll need to wrap that in
Mongoid.unit_of_work { Credential.last.token }
Mongoid caches the queries. It is not a problem for web requests, but in the console you won't see the change unless you do it in the unit of work block, or restart the console (not just a reload)
I had to put
attr_accessor :token, ...

Resources