Having a bit of trouble over here.
In the following example, Offer::PropertyInfo does not show up in the rails_admin dashboard:
class Offer
include GlobalID::Identification
include Mongoid::Document
include Mongoid::Timestamps
field :price, type: Integer
field :funding_source, type: String
field :name, type: String
field :email, type: String
field :phone, type: String
field :phone_verified, type: Mongoid::Boolean, default: false
embeds_one :property_info, class_name: "Offer::PropertyInfo"
belongs_to :property
end
class Offer::PropertyInfo
include Mongoid::Document
field :street_name, type: String
field :street_number, type: String
field :unit_number, type: String
field :city, type: String
field :state, type: String
field :zip, type: String
embedded_in :offer
end
Any ideas why not? Is it because these are in the same file? Namespaced? Using mongoid 4.0.1.
Thanks!
Have you tried creating def name street_name end in PropertyInfo class? That should display in RA.
Related
I simply can't understand why I get this error:
(models.E015) 'ordering' refers to the nonexistent field, related field, or lookup 'name'
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=255),
slug = models.SlugField()
class Meta:
ordering = ('name',)
def __str__(self):
return self.name
def get_absolute_url(self):
return f'/{self.slug}'
Ah, I guess it is the comma at the end of the name field declaration...
Remove the comma and have it this way:
class Meta:
ordering = ('name')
Then run the makemigrations && migrate commands.
add '-' ini ordering
ordering = ('-name',)
According to Spring Data Mongo docs
The following outlines what property will be mapped to the '_id'
document field:
A property or field annotated with #Id (org.springframework.data.annotation.Id) will be mapped to the '_id'
field.
A property or field without an annotation but named id will be mapped to the '_id' field.
I have some nested classes that have id field/property that does not need to mapped to mongo _id field but rather as a plain id field. Any tricks?
If you don't want that the id field is mapped to the mongo _id field you have to provide another field in your class with the#Id annotation. Then this field is used as mongo id and you can use the other in any way you want.
Example:
public class Foo {
#Id
private String mongoId;
// Your id
private String id;
}
I'm using Mongoid 3. I have a simple class Tour and references multiple Itineraries. Is there a way that I can validate that for each tour, the itineraries' dates are unique, i.e. I can't have 2 itineraries of the same date for a single tour.
class Tour
has_many :itineraries
end
class Itinerary
field :date, :type => Date
validates :date, :presence => true
index({date: 1})
belongs_to :tour
end
I'm not sure how to set up the validation.
You can create custom validations :
class Tour
has_many :itineraries
validates :check_uniqueness_of_date # This line
# And this part
private
def check_uniqueness_of_date
# Check validation here
end
end
Another Stackoverflow Question
Rails Guides
I'm having problems getting multiple ids to save properly into my database.
I have a listing that has two parameters: a listing id and a price.
I've tried changing the controller to accept the current_user, and no luck there.
I've tried changing the model, and I've also tried manually creating a listing while giving it a user_id and book_id (checking that it is, indeed, giving the correct book and user ids). In the manual listing, I've also tried to make variable names without the # symbols, and though there are no errors, I'm still unable to store the values in the database
My listing model:
class Listing < ActiveRecord::Base
belongs_to :user
belongs_to :book
has_one :order
belongs_to :user, class_name: 'Listing'
attr_accessible :listing_id, :price
end
My user model:
class User < ActiveRecord::Base
has_many :books
has_many :listings
belongs_to :creator, class_name: 'User'
end
My book model:
class Book < ActiveRecord::Base
has_one :status
has_one :listing
belongs_to :user
attr_accessible :condition, :isbn, :location, :title, :weight, :comment, :description, :price
validates :isbn, :isbn_format => true
end
The create function in my listings controller
def create
#listing = Listing.new(params[:listing])
#listing.user_id = current_user_id
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render json: #listing, status: :created, location: #listing }
else
format.html { render action: "new" }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
How I've tried to manually create the user_id and book_id columns:
<b><%= "Returned: "+doc.css("Ack").text %> </b> #returns if listing was a failure or success
<b><%= "Listing ID: "+doc.css("ItemID").text %></b> #returns the listing itemID
<% #book = Book.find_by_id(params[:book_id_num]) %><br /> #passed from a previous page
<% #user = User.find_by_id(current_user.id) %>
<%= #book.id %> #successfully displays the correct book id on the web page
<%= #user.id %> #successfully displays the id of the current_user
<% Listing.create(:listing_id => doc.css("ItemID").text, :price => price, :book_id => #book.id, :user_id => #user.id)%>
But doing it this way will only create a listing with a listing_id and a price.
I've been stuck for a while, and am unsure of how to proceed. I'm also fairly unfamiliar with how a database might function. Can anyone help me out?
I figured out the answer to my problem.
Using the rails console, it told me that it was unable to edit certain attributes in the creation of a listing.
I'm not sure if it's the rails way to do things, but if I allow the user_id and book_id to be accessible attributes, I am able to store the listing with the correct ids in the database. So my listing model looks as follows:
class Listing < ActiveRecord::Base
belongs_to :user
belongs_to :book
has_one :order
belongs_to :user, class_name: 'Listing'
attr_accessible :listing_id, :price, :user_id, :book_id
end
I can edit these values in the table appropriately when I create a listing.
Thanks to mu is too short for giving me the tip to check the creation of a listing in the rails console. I was not aware that it could be used in such a useful way.
Does anyone know how to do a polymorphic association in Mongoid that is of the relational favor but not the embedding one.
For instance this is my Assignment model:
class Assignment
include Mongoid::Document
include Mongoid::Timestamps
field :user
field :due_at, :type => Time
referenced_in :assignable, :inverse_of => :assignment
end
that can have a polymorphic relationship with multiple models:
class Project
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
references_many :assignments
end
This throws an error saying unknown constant Assignable. When I change the reference to embed, this all works as documented in Mongoid's documentation, but I need it to be reference.
Thanks!
Answering to an ancient post, but someone may find it useful.
Now there's also a polymorphic belongs_to:
class Action
include Mongoid::Document
include Mongoid::Timestamps::Created
field :action, type: Symbol
belongs_to :subject, :polymorphic => true
end
class User
include Mongoid::Document
include Mongoid::Timestamps
field :username, type: String
has_many :actions, :as => :subject
end
class Company
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
has_many :actions, :as => :subject
end
From Mongoid Google Group it looks like this is not supported. Here's the newest relevant post I found.
Anyway, this is not to hard to implement manually. Here's my polymorphic link called Subject.
Implementing inverse part of relation might be somewhat more complicated, especially because you will need same code across multiple classes.
class Notification
include Mongoid::Document
include Mongoid::Timestamps
field :type, :type => String
field :subject_type, :type => String
field :subject_id, :type => BSON::ObjectId
referenced_in :sender, :class_name => "User", :inverse_of => :sent_notifications
referenced_in :recipient, :class_name => "User", :inverse_of => :received_notifications
def subject
#subject ||= if subject_type && subject_id
subject_type.constantize.find(subject_id)
end
end
def subject=(subject)
self.subject_type = subject.class.name
self.subject_id = subject.id
end
end
Rails 4+
Here's how you would implement Polymorphic Associations in Mongoid for a Comment model that can belong to both a Post and Event model.
The Comment Model:
class Comment
include Mongoid::Document
belongs_to :commentable, polymorphic: true
# ...
end
Post / Event Models:
class Post
include Mongoid::Document
has_many :comments, as: :commentable
# ...
end
Using Concerns:
In Rails 4+, you can use the Concern pattern and create a new module called commentable in app/models/concerns:
module Commentable
extend ActiveSupport::Concern
included do
has_many :comments, as: :commentable
end
end
and just include this module in your models:
class Post
include Mongoid::Document
include Commentable
# ...
end