rails_admin throws undefined method `html_safe' for nil:NilClass - ruby-on-rails

Here's my model. Working without errors on other similar models.
Not sure even how to troubleshoot this. Any help is greatly appreciated. Thanks
class OrderProduct
include Mongoid::Document
include Mongoid::Token
validates :quantity, :numericality => { :greater_than => 0 }
before_save :pre_save
after_save :post_save
token :field_name => :code, :pattern => "%C3%d7%C2"
field :quantity, type: Integer, :default => 1
field :notes, type: String
field :fit_satisfactory, type: Boolean, :default => true, overwrite: true
field :alteration_cost, type: Float, default: 0.0
field :fit_status_saved, type: Date, default: nil
field :initials, type: String
field :last_product_type_order, type: String, default: nil
auto_increment :sequence, :seed => 10000
belongs_to :order_product_status
belongs_to :product, autosave: true
belongs_to :order
belongs_to :product_fit
has_many :product_fit_entry_values, autosave: true
embeds_many :customizations, class_name: "OrderProductCustomization"

Looks like others have encountered that same problem and posted it on the rails_admin github page:
Issue 1852
Some people have replied with work arounds, feel free to try those. However, the issue is still open so rails_admin hasn't been officially patched yet.

Related

Rails Neo4j Migration Argument Error

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

Minitest for testing presence and allow_nil for the same attribute

I want to test a model attribute where it has presence as true, but also allow nil value. How can I test this? I made an example bellow:
# Person model
belongs_to :city
validates :city_id, presence: true, :allow_nil => true
I was trying to test with:
test "should permit nil for city_id" do
#person.city_id = nil
assert #person.valid?
end
What I got in console:
Expected false to be truthy.
I found the answer. The test was failing because it was missing a argument in belongs_to:
# Person model
belongs_to :city, optional: true
validates :city_id, presence: true, :allow_nil => true
The argument Optional allows nil value to foreign key.

How can I do search category.name by using elasticsearch in Rails?

My search working fine.
But I have to type "1" or "2" to get results of "roommate" or "sublet".
Model has a column called category_id which is an integer.
Model Category has column :name which is a string.
Thus, I have category_id 1 is having "roommate" and 2 is "sublet"
below is my Housing model:
class Housing < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :user
belongs_to :category
validates :title, :presence => true
validates :category_id, :presence => true
validates :created_at, :presence => false
validates :user_email, :presence => true
validates :description, :presence => false
validates_length_of :title, :maximum => 30
def self.search(query)
__elasticsearch__.search(
{
query: {
# multi_match: {
simple_query_string: {
query: query,
fields: ['title^10', 'category_id']
}
}
}
)
end
end
How can I fix fields: ['title^10', 'category_id'] So user can search "roommate" instead of must search integer "1" to get result of roommate ?
I tried fields: ['title^10', 'category.name'] but not working.
fields: ['title^10', 'category.name'] won't work unless you have correct mapping defined. Elasticsearch doesn't know about your associations. ES is a document store and searches for records using it's own document store. So unless you add your category name to the document stored in ES, you won't be able to search it.
TL;DR
Define a mapping. Example:
mapping dynamic: 'strict' do
indexes :category do
indexes :name
end
indexes :title
end
Here category will now be stored as nested object inside your index and hence is searchable using category.name

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?

Resources