rails app - mongoid does not insert a new field - ruby-on-rails

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)

Related

MongoId combined uniqueness index

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.

Concern and field Rails

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

Ruby + Mongoid: how to make a required field?

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.

Nested attributes in mongodb confusion

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.

Rails 3: Mongoid validation issue

Using Mongoid, I am trying to validate the :code input on the submission form to make sure they are using a proper code that is already stored in the database. There are about 2000+ codes so a helper method array collection wouldn't be feasable.
What is the best way to do this?
I was thinking of doing an inclusion validation, like this:
class Request
include Mongoid::Document
field :code, type: String
validates :code, :presence => true,
:inclusion => { :in => proc { Listing.all_codes } }
end
Then the model that has all the stored codes, like this:
class Listing
include Mongoid::Document
field :code, type: String
def self.all_codes
where(:code => exists?) # <--- this is broken
end
end
But I can't seem to get this to function the way I would like. Any feedback would be much appreciated.
Your Request model looks fine. But the Listing.all_codes needs to return an array of only codes. so:
class Listing
include Mongoid::Document
field :code, type: String
def self.all_codes
only(:code).map(&:code)
end
end

Resources