Mongoid validates_presence_of a belongs_to item - ruby

How do I go about validating the presence of an item defined as belongs_to? In other words:
class Temp
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user
end
and I wish to make sure a user was input.
Thanks in advance!

From the fine manual:
The parent document of the relation should use the has_many macro to indicate is has n number of referenced children, where the document that is referenced uses belongs_to.
class Band
include Mongoid::Document
has_many :members
end
class Member
include Mongoid::Document
field :name, type: String
belongs_to :band
end
[...]
# The parent band document.
{ "_id" : ObjectId("4d3ed089fb60ab534684b7e9") }
# The child member document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7f1"),
"band_id" : ObjectId("4d3ed089fb60ab534684b7e9")
}
Take note of the band_id that represents the belongs_to :band relationship. So saying:
belongs_to :user
implicitly adds a field :user_id to your Temp. That means that you can simply:
validates_presence_of :user_id
to make sure that a :user_id has been given. If you want to ensure that the :user_id is valid then you could:
validates_presence_of :user
and the validation will make sure that temp.user (i.e. User.find(temp.user_id)) finds something.

Related

Mongoid has_and_belongs_to_many, inverse_of: :nil, store_as

In my code I have a user class:
class User
include Mongoid::Document
has_and_belongs_to_many :person_record_bookmarks, inverse_of: nil, :class_name => "PersonRecord"
end
now it will generate person_record_bookmarks_ids in my document. This name is too long, is there any way to store it as shorter name in the database? In embed documents we can use store_as:, but seems it doesn't work for references.
foreign_key is what you are looking for
class User
include Mongoid::Document
has_and_belongs_to_many :person_record_bookmarks, inverse_of: nil, :class_name => "PersonRecord", foreign_key :shorter_name
end
then your user will be:
{...shorter_name:[ObjectId("..."),ObjectId("...")]...}
You can user user.shorter_name to retrieve the list of ids or user.person_record_bookmarks to retrieve all PersonRecordBookmarks.where({_id: {$in: shorter_name})

Rails 4 Nested Validations not working

I have this model:
class CompetitionEntry < ActiveRecord::Base
has_many :participants
has_one :address
has_many :music_programs
accepts_nested_attributes_for :address
accepts_nested_attributes_for :participants, :music_programs,
:allow_destroy => true,
:reject_if => :all_blank
end
and this one:
class Participant < ActiveRecord::Base
belongs_to :competition_entry
has_one :birthplace
validates :name, :surname, :instrument, presence: true
end
Now the problem is that, if I create a new competition entry, it goes through.
But if I fill ONE field, i.e name, then it comes up with an error!
Why is this happening? It should fail when all are empty!
When you use accepts_nested_attributes_for, you are able to create the participants records at the same time that the competition_entry record, considering that the hash passed to competition_entry.create contains participants_attributes. When you pass only name, it validates the participant to be created and fail because it has no surname and instrument. When you leave all field empty, the behaviour should be the same, but it isn't because you explicitly set :reject_if => :all_blank.
:reject_if => :all_blank states that the participant_attributes hash should be ignored if it is blank?, which happens when you don't fill any field. What is happening then is that a competition_entry is being created without trying to create a participant because the accepts_nested_attributes_for is just ignored.

Mondoid search one document with multiples in a has_many/belongs_to relationship

I have this releationship
class Cupboard
include Mongoid::Document
field :name, type: String
has_many :ingredients
end
class Recipe
include Mongoid::Document
field :name, type: String
has_many :ingredients
end
class Ingredient
include Mongoid::Document
field :name, type: String
field :description, type: String
belongs_to :cupboard
belongs_to :recipe
end
I need to create a method in Cupboard model to find a recipe that contains the same ingredients as the cupboard, I don't find in the Mongoid docs a method to find it.
I need something like Recipe.find( #all cupboard.ingredients.ids )
thanks in advance
def shared_recipes
ingredients.map(&:recipe).uniq
end

Mongoid: blank belongs_to association

I have belongs_to :provider in my model, and provider_id is set, but association is still blank:
irb(main):095:0> o2.provider_id
=> BSON::ObjectId('4e0472f36d40ec0004000001')
irb(main):096:0> o2.provider
=> nil
Also, model has provider_id= method with "write_attribute(:provider_id, provider_id)" inside.
What could be wrong here?
Mongoid version is 2.4.0
Thank you!
I believe you need a has_one or has_many association in your other model.
class Foo
include Mongoid::Document
belongs_to :bar
end
class Bar
include Mongoid::Document
has_one :foo
end

Mongoid 1..N polymorphic referenced relations

I have the following models
class Track
include Mongoid::Document
field :artist, type: String
field :title, type: String
has_many :subtitles, as: :subtitleset
end
class Subtitle
include Mongoid::Document
field :lines, type: Array
belongs_to :subtitleset, polymorphic: true
end
class User
include Mongoid::Document
field :name, type: String
has_many :subtitles, as: :subtitleset
end
And in my ruby code when I create a new Subtitle I'm pushing it in the appropriate Track and User like this:
Track.find(track_id).subtitles.push(subtitle)
User.find(user_id).subtitles.push(subtitle)
The problem is that it gets pushed only in the User and not in the Track also. But if I remove the second line it gets pushed it the Track. So why isn't working for both?
I get this in the Subtitle document:
"subtitleset_id" : ObjectId( "4e161ba589322812da000002" ),
"subtitleset_type" : "User"
If a subtitle belongs to something, it has an ID pointing to that something. A subtitle cannot belong to two somethings at once. If the belonging is polymorphic, a subtitle can belong to something whose class is not specified - but it still cannot belong to two somethings at once.
You want:
class Track
include Mongoid::Document
field :artist, type: String
field :title, type: String
has_many :subtitles
end
class Subtitle
include Mongoid::Document
field :lines, type: Array
belongs_to :track
belongs_to :user
end
class User
include Mongoid::Document
field :name, type: String
has_many :subtitles
end
And then you will be able to:
Track.find(track_id).subtitles.push(subtitle)
User.find(user_id).subtitles.push(subtitle)

Resources