So my data is pretty simple. Books and Users.
class Books
belongs_to :user
end
class Users
has_many :books
end
Users has the following fields:
first_name
last_name
email
Books has the following fields:
author
title
description
My book_type looks like:
Types::BookType = GraphQL::ObjectType.define do
name 'Book'
field :id, !types.ID
field :author, !types.String
field :title, !types.String
field :user, -> {Types::UserType}, property: :user
end
User_type looks like:
Types::UserType = GraphQL::ObjectType.define do
name 'User'
field :id, !types.ID
field :first_name, !types.String
field :last_name, !types.String
field :email, !types.String
end
Mutation_type looks like:
Types::MutationType = GraphQL::ObjectType.define do
name 'Mutation'
field :bookCreate, function: Resolvers::BookCreate.new
end
Book_create file looks like:
class Resolvers::BookCreate < GraphQL::Function
argument :author, !types.String
argument :title, !types.String
argument :description, !types.String
argument :user, !types.Int
type Types::BookType
def call(_obj, args, _ctx)
Book.create!(
author: args[:author],
title: args[:title],
description: args[:description],
user: args[:user]
)
end
end
Then in localhost:3000/graphiql I do the following:
mutation book {
bookCreate(
author: "Test",
title: "Book Test",
description: "Book Description",
user: 2) {
id
author
title
description
}
}
Getting the following:
<ActiveRecord::AssociationTypeMismatch: User(#69982231587660) expected, got 2 which is an instance of Integer(#11387600)>
Where did I go wrong?
So I changed user to user_id and then on the book model did:
belongs_to :user, foreign_key: 'user_id'
Now it works.
Related
here's a list of model and their relation below:
class Section
has_many :students, as: :resource
searchable do
integer :id
join(:first_name, prefix: "student", target: Student, type: :text, join: {from: :resource_id, to: :id})
join(:last_name, prefix: "student", target: Student, type: :text, join: {from: :resource_id, to: :id})
end
end
class Student
belongs_to :resource, polymorphic: true, optional: false
has_many :contact_number, as: :resource
searchable do
text :first_name
text :last_name
integer :id
integer :resource_id
string :first_name
string :last_name
end
end
class ContactNumber
belongs_to :resource, polymorphic: true, optional: false
end
as you can see in my class model Section has many students. I can search student "first_name" and student "last_name" because of the help of joins. is there possible way to search student contact numbers. using of joins??? or what is the workaround to search the contact numbers in Section model?
in my ContactNumber model, i create an another joins
class ContactNumber
searchable do
string :ref_id do
if resource_type == "Student"
[resource.resource_type, resource.resource_id].join("_").downcase()
end
end
end
in my Student model
searchable do
string :ref_id do
[resource_type, resource_id].join("_").downcase()
end
join(:content, prefix: "contact_number", target: ContactNumber, type: :text, join: {from: :ref_id, to: :ref_id})
end
last in my Section class
class Section
searchable do
string :ref_id do
[self.class.name, id].join("_").downcase()
end
join(:content, prefix: "number", target: ContactPerson, type: :text, join: {from: :ref_id, to: :ref_id})
end
end
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
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
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?
I have a problem with NoMethodError in one of my models.
In the log file, we have:
NoMethodError (undefined method `length=' for #<Book:0x000000083866b8>):
2013-03-28T10:25:19+00:00 app[web.1]: app/models/engine/book.rb:13:in `block in find_or_create_by_guide'
2013-03-28T10:25:19+00:00 app[web.1]: app/models/engine/book.rb:9:in `find_or_create_by_guide'
Let me go through all of the important files.
For a start, we have Mongo's document.rb:
class Guide::Document
include MongoMapper::Document
key :city, Integer
key :trips, Array
key :themes, Array
key :places, Array
key :pace, String
key :date_from, Time
key :date_to, Time
key :host, String
key :length, Integer
timestamps!
end
Then, the book model is called upon the guide document:
module ClassMethods
def find_or_create_by_guide(guide)
book = ::Book.find_or_create_by_document(guide.id.to_s) do |b|
b.city_id = guide.city
b.host = guide.host
b.pace = guide.pace || :normal
b.length = guide.length
end
later in the book.rb, I have the following line:
groups = sorted_points.in_groups_of(self.length.count, false)
Length.rb:
class Length < ActiveRecord::Base
belongs_to :book
attr_accessible :book_id
end
Book.rb:
attr_accessible :user_id, :country_id, :city_id, :hotel_id, :type, :price, :host, :pace, :created_at, :updated_at, :length
Finally, the migrations of Length:
class AddLengthColumnToBooks < ActiveRecord::Migration
def change
add_column :books, :length, :integer
end
end
Any hints or tips appreciated.
This is a mess. Once you want 'length' to be an attribute of Book, once you want Length to be a separate model which is in a relation with Book.
I see no point in having Length model.
Go with 'length' as a Book property.