I use the githead version of Mongoid (because of Rails 4) and I want to make a field required document with:
class MyClass
include Mongoid::Document
field :name, type: String, required: true
And I have this error:
Problem: Invalid option :required provided for field :name. Summary: Mongoid requires that you only provide valid options on each field definition in order to prevent un...
What am I doing wrong?
You need to use validates_presence_of, so your class would look like this:
class MyClass
include Mongoid::Document
field :name, type: String
validates_presence_of :name
end
For further documentation on validation on mongoid, you can use http://mongoid.org/en/mongoid/docs/validation.html.
Related
I just started to create on app with rails-api and mongoDB (gem mongoid in rails).
I have created my model like this:
class User
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :language
accepts_nested_attributes_for :language
field :name, type: String
field :lastname, type: String
field :mail, type: String
field :passwd, type: String
field :auth_token, type: String
end
And i want to add another field in my model.
So at the end of my model i add this:
field :slug, type: String
But when i insert a new document, mongoid doesn't detect the new field and return null.
I try to do rake:migration but is useless with mongodb and i can't find the issue. Could you help me?
Best regards
Most probably you did not add that new field into your strong params whitelist.
Look into your controller and find a line that looks like this:
params.require(:user).permit(:name, ...)
Add slug there like this:
params.require(:user).permit(:name, ..., :slug)
I have three models
class Org
include Mongoid::Document
field :name, type: String
embeds_many :org_groups
end
class OrgGroup
include Mongoid::Document
field :name, type: String
embedded_in :org
has_and_belongs_to_many :humans
end
class Human
include Mongoid::Document
field :name, type: String
end
One Human can be in many Org, but only in one OrgGroup.
I need set uniqueness index for Human in Org.
How I can do this?
You can create a method that will be call by a callback.
See documentation for callbacks.
You can simply raise something from this method if your conditions are not respected.
Ask if you need a sample.
If you need a unique index in the mongodb, you can do like this:
class Person
include Mongoid::Document
field :first_name
field :last_name
index({ first_name: 1, last_name: 1 }, { unique: true })
end
And the docs are here:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-indexes/
Hope this is helpful for you.
i want to create a concern like this:
module Geolocalizable
extend ActiveSupport::Concern
include Mongoid
included do
attr_accessible :lat, :lng
field :lat
field :lng
end
end
And than include it in my model :
class Store
include Mongoid::Document
include 'Geolocalizable'
field :name, type: String
field :address, type: String
end
But in my stores/new.html.erb
this line gives me an error
f.text_filed :lat
undefined method `lat' for #Store:0xae6bb58
How can i solve this?
I think it's wrong argument type, it should be: include Geolocalizable
I am using rails with database Mongodb.
I am using devise . Devise has model name user.The id of the user is in
:id => current_user.id
I want to make a model such that when data is save from the form the id of the current user will also save in the collection.
The model of my employee is
class Employee
include Mongoid::Document
field :first_name, type: String
field :middle_name, type: String
field :last_name, type: String
field :otherid, type: String
field :licennum, type: String
field :citizennum, type: String
field :licenexp, type: String
field :gender, type: String
field :state, type: String
field :marital_status, type: String
field :country, type: String
field :birthdate, type: String
field :nickname, type: String
field :description, type: String
validates_presence_of :first_name
{
}
end
what should i put inside curly bracket so that when data is saved from that model it saves the current user id also inside it?
The code you show only contains personal information fields like birth date and such. I suppose the simplest solution would be to place them inside the User class, and change them using built-in devise actions such as devise/registrations#edit, which applies the changes to current_user as default.
Alternatively, if you want to keep Employee as a separate class, you could try embedding Employee in User class, like this:
class User
include Mongoid::Document
embeds_one :employee
(...)
class Employee
include Mongoid::Document
embedded_in :user
In this case, you would set up the relation at controller level, e.g.:
class EmployeesController < ApplicationController
def create
current_user.create_employee(params[:employee])
end
After creation, you could access the user's ID from Employee class as user.id.
I have a class that looks like this:
class SearchService
include Mongoid::Document
key :name, String
field :url, String
field :searchBaseUrl, String
validates_presence_of :name, :url, :searchBaseUrl
validates_uniqueness_of :name
end
The first issue here is that I was using validates_presence_of incorrectly or so it would seem. I commented the line out and I could create the class instance at the console with no problem. But when I tried to create the object with validates_presence_of in place I got an error:
NoMethodError: undefined method `delete' for String:Class
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.3/lib/mongoid/fields.rb:230:in `add_field'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.3/lib/mongoid/fields.rb:145:in `field'
Is this error message meaningful? I'm having a hard time seeing the relationship between a missing method on the String class and me using validates_presence_of incorrectly. If I could pick only one thing about Ruby that bugs me it would have to be terrible error messages.
The rails docs say that validates_presence_of is for association. But the mongo db page says that it's used for required fields. Are the mongodb docs incorrect?
I'm trying to decide if this is an issue I should report to the mongoid team or if I'm just not understanding how the language works.
Update: OK now I'm getting a very similar error in a class that doesn't use validates_presence_of at all. So it's some other issue (even though commenting out that line fixed it initially).
Update2: This appears to me to be bug in mongoid. Changing this line:
field :name
to
field :name, String
will cause the error to show up. Seems that any field that is defined with a type will mess up mongoid. Or those aren't supposed to be there anymore? The mongodb docs (linked above) have code that looks like key :votes, Integer, :default => 0 so if it isn't valid the docs are wrong.
Rails 3.1.1, Ruby 1.9.3
You use incorrect syntax for field type definition. It should use options hash and key "type". Example from from documentation http://mongoid.org/
class Artist
include Mongoid::Document
field :name, type: String
embeds_many :instruments
end
You refer to MongoMapper examples, but use Mongoid ))
jcollum, the problem is coming from the fact that you are using mongoid but the examples on mongodb.org are using mongomapper.
Please be aware that these are different ODMs for rails and have slightly different syntax for defining fields.
Please see the documentation here for mongoid.
The format for the fields using mongoid:
class Person
include Mongoid::Document
field :first_name, type: String
field :middle_name, type: String
field :last_name, type: String
end
In Rails 3, you should be using the new style validations:
validates :name, :presence => true, :uniqueness => true
validates :url, :presence => true
validates :searchBaseUrl, :presence => true
Use with_options to make it DRYer
with_options :presence => true do |v|
v.validates :name, :uniqueness => true
v.validates :url
v.validates :searchBaseUrl
end