I've got this stupid thing... I'm sure I just miss something obvious but yahoogling didn't solve the problem.
All I do is
rails new TestApp
and
cd TestApp
rails generate scaffold User name:string age:integer
bundle exec rake db:create
bundle exec db:migrate
which works fine.
But when I go to the IRB, there is no User!
u = User.first
NameError: uninitialized constant User
from (irb):3
from /usr/bin/irb:12:in `<main>'
What's wrong here?
Cheers
Don't use irb, instead:
rails console
which will have every model of your project imported.
You haven't created a user and you are using plain old irb instead of rails console.
open a rails console and try:
User.create(:name => "Jimmy", :age => 14)
Then try
u = User.first
Related
I am a brand new Ruby on Rails User
When I enter localhost:3000 ,
I get an error reading
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
You have 2 pending migrations:
20221119205559_add_devise_to_users.rb
20221119211811_drop_users_table.rb
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
end
def load_schema_if_pending!
In a nutshell, it was working before but I attempted to create a a basic authentication page(which was also working) , but for some reason when I clicked sign up I received an error also.
Thank you for any tips on how to fix this!
I have tried to
rake db:drop
rake db:create
rake db:migrate
Editing the migrate file with:
edit your migration file
class DropUsersTable < ActiveRecord::Migration
def change
drop_table :users
end
end
Then doing rake db:migrate
Also, I have run :
rails generate devise:install
rails generate devise User
bundle exec rake db:migrate
I enabled Refinery Search and Refinery Blog extensions in Gemfile and run rake db:migrate, rake db:seed.
And per Refinery Search instructions file, added the following to config/application.rb
config.to_prepare do
Refinery.searchable_models = [Refinery::Blog]
end
And also created app/decorators/models/refinery/blog_decorator.rb with
Refinery::Blog.class_eval do
acts_as_indexed :fields => [:title, :body, :custom_teaser]
end
Above example is from: http://refinerycms.com/guides/extending-model
But when I try to run rails c or rails s, there is an error:
=> Ctrl-C to shutdown server
Exiting
/home/bismailov/Desktop/my_docs/Inbox/ror/maqolarefinery/app/decorators/models/refinery/blog_decorator.rb:2:in `block in <top (required)>':
undefined method `acts_as_indexed' for Refinery::Blog:Module (NoMethodError)
from /home/bismailov/Desktop/my_docs/Inbox/ror/maqolarefinery/app/decorators/models/refinery/blog_decorator.rb:1:in `class_eval'
What could I be missing here? Thank you a lot!
Actually Refinery::Blog is a module not a model. Ideally you should use Refinery::Blog::Post.
I am new to RoR. I have two problems here.
ISSUE 1
my rspec is
require 'spec_helper'
describe RefUserCategory do
describe "validations" do
it { should validate_presence_of(:user_category_description) }
end
end
my Ref_User_Category model is
class RefUserCategory < ActiveRecord::Base
validate :user_category_description , presence: true
has_many :Users
end
The error I got in Rspec is
Expected errors to include /can't be blank/ when user_category_description is set to nil, got no errors
So I decided to to use Pry to check whats happening
ISSUE 2
Im my application folder I do
pry -r ./config/environment
[2] pry(main)> Ref_User_Category.new
LoadError: Unable to autoload constant Ref_User_Category, expected /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/736003/east/app/models/ref_user_category.rb to define it
from /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/lib/ruby/gems/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:464:in `load_missing_constant'
I have user model too I
pry(main)> User.new
gives error
ActiveRecord::StatementInvalid: Could not find table 'users'
/app-root/data/lib/ruby/gems/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:512:in `table_structure'
Regarding issue 1, you have a typo: change validate to validates.
Regarding issue 2:
You generally start the console from the root of your project like this:
$ rails c
You are doing Ref_User_Category.new when you should do RefUserCategory.new.
About User.new, it looks like you haven't run your database migrations:
$ rake db:migrate
$ rake db:test:prepare
I defined a model in rails using command rails generate model testdetails . After that I went to db/migrate and wrote the fields for this model
class CreateTestDetails < ActiveRecord::Migration
def self.up
create_table :test_details do |t|
t.column :TestName ,:string
t.column :TestType ,:integer
end
end
end
then i did db:migrate , it throws some error . I google it and found out that it may be coming due to devise version ( I am using devise for authentication) , I updated Gemfile and wrote the version of devise (2.1) and did bundle install . After that I again did db : migrate but it is showing this error
rake aborted!
undefined method secret_key=' for Devise:Module
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:7:inblock in '
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:3:in <top (required)>'
/home/vibhor/rails_projects/recruit/config/environment.rb:5:in'
Tasks: TOP => db:migrate => environment
what should i do so that this model can be created without any error? i am using rails 3.2.13 and ruby 2.0.0
I think this is due to gem version so update it to latest version 3.x or remove that line from your config/initializers/devise.rb file.
In your config/initializers/devise.rb file add this line:
config.secret_key = 'Your secret Key'
And use rake secret to generate your secret key.
There's an issue open in github if you need more info.
Had the same problems lately. Check devise initializers, as it has changed lately. For me it solved the problem.
I try to create User instance of my user model in irb. but it gives me this error
1.9.3p194 :001 > u = User.new
NameError: uninitialized constant User
from (irb):1
from /home/darshana/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
any help?
irb doesn't loads rails for you.
try bundle exec rails console