Concern and field Rails - ruby-on-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

Related

Migrating from active record/postgres to mongodb/mongoid and I cannot create/save

I have the following models in mongoid(i.e., was previously in active record).Trying save product item group along with item selection option but I am running into no method "all_of" error.And I am new to using mongoid/mongobd with ROR.
class ProductItemGroup
include Mongoid::Document
include Mongoid::Timestamps
store_in database: "cnc_product_ms"
field :name, localize: true
field :description, localize: true
field :product_item_selection_type, type: String
embeds_one :item_selection_option
has_and_belongs_to_many :service_categories
field :service_category_ids, type: Array
end
class ItemSelectionOption
include Mongoid::Document
include Mongoid::Timestamps
include SimpleEnum::Mongoid
store_in database: "cnc_product_ms"
as_enum :selection_type,multiple: 1, single: 2
as_enum :mandatory_selection_type, atleast_one: 1, many: 2
field :selection_type, type: Integer
field :selection_mandatory, type: Boolean, default: false
field :mandatory_selection_type, type: Integer
embedded_in :product_item_group
end
controller /form
class ProductItemGroupForm < ApplicationForm
attr_accessor(:product_item_group)
def create
#product_item_group = ProductItemGroup.new(params)
#product_item_group.save!
end
end
on save!, I am running into following error
NoMethodError: undefined method `all_of' for #<Class:0x00007fd96afea600>
from /Users/90srebel/.rvm/gems/ruby-2.6.4/gems/activerecord-6.0.3.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
Got the issue. I am closing this question.
What I did wrong was,
I declared has_and_belongs_to_many in product_item_group model but forgot to declare the same thing in service_category model. Silly

rails app - mongoid does not insert a new field

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)

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.

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.

mongoid embeds_many as with concern

I'm trying to build a model for annotations like the one App.net has: http://developers.app.net/docs/meta/annotations/. To easily add the annotations to many models I use a concern.
So far I have the model for the Annotation:
class Annotation
include Mongoid::Document
field :key, :type => String
field :value
embedded_in :annotatable, polymorphic: true
# Some validations
attr_accessible :key, :value
attr_accessor :key, :value
end
The concern:
module Annotatable
extend ActiveSupport::Concern
included do
embeds_many :annotations, as: :annotatable
end
# Somewhat helper method to add annotaion only after they are valid.
def add_annotation( annotation )
if !annotation.valid?
return false
else
self.annotations.push annotation
return true
end
end
end
And finally there's a model where I include the concern:
class User
include Mongoid::Document
include Mongoid::Timestamps
include Annotatable
# Much other stuff...
end
Unfortunately the annotations won't get saved :-/
Any ideas?

Resources