Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:54:01+00:00 2026-05-26T02:54:01+00:00

I have download the template Rails 3 + Mongoid + Devise and installed. I

  • 0

I have download the template Rails 3 + Mongoid + Devise and installed.

I have create a scaffold Car for relation with User model of Devise. I have in my User model this code:

class User
  include Mongoid::Document
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and       :omniauthable
  devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

  field :name
  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me

  embeds_many :cars

  end

and My Cars Model I have the next code:

class Car
  include Mongoid::Document
  field :title
  field :description
  field :image_url
  field :price
  field :published_on, :type => Date
  validates_presence_of :title, :description, :image_url, :price
validates :title, :length   => { :maximum => 70 }
validates :description, :length   => { :maximum => 2000 }
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :image_url, allow_blank: true, format: {
  with:
  %r{\.(gif|jpg|png)$}i,
  message: 'must be a URL for GIF, JPG or PNG image.'
  }

  embedded_in :user, inverse_of: :cars

end

When I refresh the page I get the next error:

Mongoid::Errors::InvalidCollection in Cars#index

Access to the collection for Car is not allowed since it is an embedded document, please access a collection from the root document.

What its the problem in this code? Thankyou

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:54:01+00:00Added an answer on May 26, 2026 at 2:54 am

    There is nothing wrong with your model but the routes and controller actions that the scaffold generated are attempting to run a query on the Cars collection directly and because Cars are embedded in User you cannot do that with Mongoid as the error message indicates. As it stands Cars are only accessible through a User object.

    There are a couple of possible ways around this. First, without changing the model, you would need to alter the routes and actions. With this model (Cars embedded in Users) it probably makes sense to use a nested route:

    resources :users do
      resources :cars
    end
    

    This will mean lead to the URL users/:user_id/cars mapping to the index action in the CarsController which might look something like this:

    def index
      user = User.find(params[:user_id])
      @cars = user.cars
      # code to render view...
    end
    

    The important point here is that you are accessing the cars for a given user. The same principle applies to the other actions.

    The second option would be to change your model to use a referenced relation rather than an embedded one but if the model is right the way it is then its better to change the controller and routes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have download and installed Visual Studio 2010 RC and I want to develop
The idea it's very easy, i have an http download class, this class must
I'm looking to take an existing git repository where I have some code, download
I have a controller action that allows a user to download a file with
I have download a simple template system and put it to my codeigniter project,
The workflow is like this: we download a template form, prefill values which will
I have 2 models which look like that: class Entry(models.Model): user = models.ForeignKey(User) dataname
I have this: <form action='/ltsp' method='GET'> <label>Download Option:</label> <select name=''> <option name='download' value='download'>download</option> <option
I've download SharpArchApplicationTemplate_1_6_VS2010.zip I have read through this: http://wiki.sharparchitecture.net/VSTemplatesAndCodeGen.ashx However, this is aimed at
I have a download page where there are 3 download options: Word, Zip, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.