issue using rails console building a Rails 5 api - ruby-on-rails

I'm building an API with rails 5.
When I try to store a user with the Rails console I get the following error:
I cannot figure out what is the problem.
I've run the migration
set the model
added the AMS gem
added serializers
added mime types
This is my schema

In app/models/user.rb file, it must be ApplicationRecord, not applicationRecord.

Related

Apartment: uninitialized constant Tenant (NameError)

I am using the Apartment Gem for the first time for Multitenancy in a Ruby on Rails project. I am trying to create multiple tenants for users in your digital library Rails application.
I am using Devise Gem for the authentication of the application, and I have generated the and I have generated config file by running the code below in my terminal
rails generate devise:install
I have also generated a User model for Devise using the code below in my terminal
rails generate devise User
And for the Apartment Gem, I have installed it and generated the config file by running the code below in my terminal
bundle exec rails generate apartment:install
I have also configured the config/initializers/apartment.rb initializer file as needed using the documentation provided, but when I run the command below in my terminal
rails generate devise:views
to generate the views for Devise, I get the error below
uninitialized constant Tenant (NameError)
I have tried to figure out the cause of the issue, but I still haven't been fortunate to get it fixed. I need some help. Thank you in advance.
I later realized that the issue was from the configuration that I made initially to Apartment Gem's config/initializers/apartment.rb initializer file.
After creating the User model, I also added it to the excluded models list in the config/initializers/apartment.rb initializer file.
config.excluded_models = %w[Tenant User]
I was supposed to instead replace Tenant in the excluded models list with the User model.
config.excluded_models = %w[User]
And that fixed the issue for me.
N/B: If I named the model name of my subdomain for the multiple tenants as Tenant, then I the configuration can be left like this
config.excluded_models = %w[Tenant]
But for my case, I named the model name of my subdomain for the multiple tenants as User
That's all.
I hope this helps

Error regarding ActiveAdmin in Ruby on Rails when customizing the menu

Mac OS X 10.7.3 Lion,
Ruby 1.9.2,
Rails 3.2.2,
Sass 3.2.3
Following this tutorial:
http://activeadmin.info/documentation.html
Following this video tutorial
http://www.youtube.com/watch?v=tAxlrHcEg9U
I add the activeadmin gem, run bundle install, then run
rails generate active_admin:install
rails generate active_admin:resource POST
Only after creating the app/admin/posts.rb and trying to run either
db migrate
rails server
fails with the error
uninitialized constant Post NameError
with out that posts.rb file i am able to run the admin interface error free.
I tried moving the sass-rails gem out side of the :assets in my gem file and re-running bundle install as suggested in another question, but to no avail I still have the error
according to the getting started active admin tutorial "Post" is suppose to be a module name so i assume the code above is calling a class method (ActiveAdmin as the class, register as the method) and sending the module as a parameter and the block do end
Regardless the error is implying that RoR doesn't know what Post is. As if it does not exist. Being new to rails i do not know how to navigate well, meaning i do not even know where this ActiveAdmin source file is in order to dig through it for a method Post
Thank you for the consideration and your time, I appreciate it.
The linked tutorial assumes that you have already created a model named Post (and have run rake db:migrate to link it to the database). The purpose of the rails generate active_admin:resource Post command is to tell ActiveAdmin that you want it to consider the Post model in part of what it does.
Historically, you'll see models like Post and User in Rails a lot -- these are the commonly used examples of creating a blogging application (a user can create blog posts).
So, whatever models you have in your application can be registered with ActiveAdmin by replacing Post with the name of your model.
Another note: while generators like this tend to be forgiving, a Post is a model that is defined in post.rb and is linked to a SQL table called posts. Be careful with things like upper- and lower-case, and singular and plurals. In Rails they all fit together in a special way.

Ruby Gem Development - How to use ActiveRecord?

I'm currently trying to develop my first ruby gem and I'm already stuck. I used the "bundle gem" command to create the basic structure and read some tutorials but what I can't find is how to integrate ActiveRecord.
Where do I create my migrations?
Do I create the "db/migrations" folder within the lib folder or at the root?
And do I have to do anything in the Rakefile (I found some questions where the answer was something like "you have to create your own [my_gem]:db:migrate" or something like that.)
All I need is a way to create a gem, which defines ActiveRecord models (including the migrations of course), which can then be used by a rails app.
Any help on that one would be greatly appreciated!
Greetings, Flo
When building a gem to integrate with a rails project, you want to build a railtie engine. If you are using rails 3.0.x, use enginex, if you are using rails 3.1 you should be use the new generator:
rails g plugin new your-plugin-name
Then, inside your gem, you can just define models, inside the app/models/ folder and they will automatically be picked up.
Migrations is somewhat harder: for rails 3.1 it is ok if you define them in the correct folder, in rails 3.0 you will have to manually generate a task to copy the migrations to your code-base. Check this link where I answered that very question.
For more information on rails engines check this and this article.
getting the functionality of ActiveRecord can be done by:
require "rubygems"
require "active_record"
class User < ActiveRecord::Base
end
This should work.

Infer associations/validations from existing database in Rails 3

Does Magic Model Generator work under Rails 3.0? I can't make it work, the install instructions that I found, doesn't help me.
Does exist an alternative to Magic Model Generator that can infer validations and associations from an existing database?
Thanx!
Looks like the MMG is abandoned -- it doesn't even work for Rails 2.3. However -- you could create a Rails 2.2 app and use it with MMG, then use the models in your Rails 3 app.
http://github.com/drnic/magic_model_generator

How can i generate mongoid.yml config in Rail 2.3.5?

As the title says, how can i generate the default mongoid.yml config file on Rail 2.3.5?
I try to use the ´rails generate mongoid:config´ command but it just generates a new app.
And also, I would like to use has_many in mongoid without embedding the associated model in the same field. I would like them to be in separate fields and associated through a *_id "column". Is that possible?
You can't. The master branch of MongoID is only Rails 3 compatible. If you want use mongoID with Rails 2.3.5, you need using the 1.0.x branch of MongoID.
In this version, there are no mongoid generator. You need define you own initializer to connect with MongoDB.

Resources