I have a 2 level nested form (much like this) with the following classes. The problem I have is that when I don't add any intervals (the deepest embedded document) I don't want the second deepest document to be persisted either. In the owner I added a reject statement to check if there's any intervals being passed down, this works.
However, when the schedule originally had intervals but they where destroyed in the form (by passing _destroy: true) the schedule also needs to be destroyed. What would be the best way to do this? I would like to avoid a callback on the schedule that destroys the document after it is persisted.
class Owner
include Mongoid::Document
embeds_many :schedules
attr_accessible :schedules_attributes
accepts_nested_attributes_for :schedules, allow_destroy: true, reject_if: :no_intervals?
def no_intervals?(attributes)
attributes['intervals_attributes'].nil?
end
end
class Schedule
include Mongoid::Document
embeds_many :intervals
embedded_in :owner
attr_accessible :days, :intervals_attributes
accepts_nested_attributes_for :intervals,
allow_destroy: true,
reject_if: :all_blank
end
class Interval
include Mongoid::Document
embedded_in :schedule
end
Update: Maybe this is best done in the form itself? If all intervals is marked with _destroy: true, also mark the schedule with _destroy: true. But Ideally the solution would be client agnostic.
How about adding this to the Owner class:
before_update do
schedules.each |schedule|
schedule.destroy if schedule.intervals.empty?
end
end
Related
The default inheritance method for Mongoid is creating a single collection for all subclasses. For instance:
class User
include Mongoid::Document
end
class Admin < User
end
class Guest < User
end
Internally, Mongoid adds a _type field to each document with the class name, which is used to automatically map each instance to the right class.
The problem is, if I have a document in this collection with an unknown _type value, I get an exception:
NameError: uninitialized constant UnknownClass
This can happen if you create a new subclass of User, in the example above, and a migration that creates a new instance of this new subclass. Until you restart your servers, every query to this collection (like User.all.to_a). Is there a safe way to avoid this error?
The only solution I came up is rescuing NameError exception and querying by all known subclasses:
class User
def self.some_query(params)
self.where(params).to_a
rescue NameError => e
Rails.logger.error "Unknown subclass: #{e.message}"
subtypes = self.descendants.map(&:to_s)
self.where(params.merge(:_type.in => subtypes)).to_a
end
end
class Part
include Mongoid::Document
belongs_to :type
# data
end
class Type
include Mongoid::Document
has_many :parts
field :name
# data
end
Let's say I have a design like the above. I need to have it in such a way that Type exists independently of any Part, both because this is the logical structure and because Types can change and I want to be able to do that centrally. But when I find a Part, I'd like to have it's associated Type embedded in the document returned. I want to do this because in almost any case I need to use the data in a Part, I need the data from its associated Type. When I get the JSON on my client side, it only containes type_id but no Type data. Is there any automatic what to include the data from Type in Part's JSON while retaining the benefits of references?
In the comments, mu answered that one can just override the as_json method of the model.
My implementation is as follows:
class Part
include Mongoid::Document
belongs_to :type
def as_json options = {}
if (options.has_key? :include and not options[:include].has_key? :type) or
not options.has_key? :include
options[:include] = :type
end
super.as_json(options)
end
end
I've a class Subscriber which has embeds_many Subscriptions. Subscription has an attribute status. I want to add a validation on status such that only one Subscription can have status 'active' per subscriber. The subscriber can have multiple subscription with status 'purchased' or 'expired' .
This should do it:
class Subscriber
include Mongoid::Document
embeds_many :subscriptions
validate :active_subscriptions
def active_subscriptions
self.errors.add(:base, 'too many active subscriptions') if
subscriptions.where(status: 'active').count > 1
end
end
class Subscription
include Mongoid::Document
embedded_in :subscriber
field :status, class: 'String'
end
s = Subscriber.create
s.subscriptions.build(status: 'active')
s.save # fires validations on subscriber
s.subscriptions.build(status: 'active')
s.save # wouldn't save
But make sure that you always call save on subscriber, otherwise the validations will not fire on subscriber and you will land in an inconsistent state. In an inconsistent state you might see failing validations later
s = Subscriber.create
s.subscriptions.create(status: 'active') # fires validations on subscription only
s.subscriptions.build(status: 'active').save # fires validations on subscription only
If you need to also validate subscription, when saving subscriber, you cascade callbacks:
embeds_many :subscriptions, cascade_callbacks: true
Is it possible to do a one way reference in mongoid?
I would like to do something like:
class User
include Mongoid::Document
include Mongoid::Timestamps
has_many :blogs, :class_name => "Blog", :inverse_of => :editor
has_one :active_blog, :class_name => "Blog", :inverse_of => :active_users
end
and the blog model:
class Blog
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :editor, :class_name => "User", :inverse_of => :blogs
end
So, basically, I would like the User to store an object id referencing the blog that it is currently editing/posting to. I do not need the blog to know about the active users, only the other way around.
It seems like the canonical way to do this would be to use 'belongs_to' on the User, and 'has_many' on the Blog. This does work, but it isn't ideal because it doesn't really semantically express the relationship between the two models.
I am new to Mongoid, and have not been able to find a better answer. Is there a better way to set up this type of releationship?
Thanks a ton!
If you do not even want to create even the accessor active_user on blog side, you can have:
class User
belongs_to :active_blog, :class_name => "Blog", :inverse_of => nil
end
On the other hand has_many/has_one and belongs_to seems perfectly fine to me. It would not store user_ids in blog and blog doesn't need to know about active user unless you decide it should and start using the accessor from blog side.
I'm using declarative authorization and mongoid. I have association as
Class Company
embeds_many :divisions
end
Class Division
embedded_in :company
end
and route as
resources :companies do
resources :divisions
end
I added filter_resource_access in my divisions_controller.rb. Permission for division index is ok, but while editing and updating declarative authorization query with division id raising error Document not found for class Division with id(s) 13244adsf32gag3.
How would I make it work with embedded controller and documents too?