Can't see data on the rails console but exists on browser - ruby-on-rails

I load my website on development mode on localhost:3000 and I can see that there are data like users, portfolios, etc.
But when I open the rails console, I can't see the data. I get empty arrays:
r00t$ rails console development
Loading development environment (Rails 4.0.3)
1.9.3-p484 :001 > User.all
User Load (1.3ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
1.9.3-p484 :002 > Portfolio.all
Portfolio Load (0.7ms) SELECT "portfolios".* FROM "portfolios"
=> #<ActiveRecord::Relation []>
1.9.3-p484 :003 >
I opened console with RAILS_ENV=development rails console too and I still get empty arrays...
Any clue? Thanks

I guess your problem could be that you're consulting different databases from the console and from the browser. Try to check if you add an object from the console if it is present also in the database you'e using or if by mistake you created a different database.

Related

Rails C command switches to inspect mode and doesn't work after that

I am new to ruby on rails and I was following a tutorial and he used rails c to open the console to edit the database and add new things I wanted to do the same but it switches to inspect mode and when I type Book.last for example, this is what happens:
(0.6ms) SELECT sqlite_version(*) Book Load (0.2ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT ? [["LIMIT", 1]] (Object doesn't support #inspect)
and If I type Book.first, it tells me 'Maybe IRB Bug' I've been searching for days and I still can't find the solution for it.
I am using Windows 10, Gitbash.

Activerecord Model.table_exists? does not give correct results

When creating the actual database table in rails console, User.table_exists? does not give me the correct result.
But when I look into ActiveRecord::Base.connection.tables it shows that the table exists. I have to exit rails console and go back in before User.table_exists? give me the correct value. Is there some caching going on? A bug in Rails 5.2.1? Or am I missing something?
Code to reproduce:
in terminal/bash
rails generate model User name:string
rails console
in rails console
User.table_exists? => false
ActiveRecord::Base.connection.tables => []
ActiveRecord::Migration[5.2].create_table :users =>
-- create_table(:users)
(0.1ms) SELECT sqlite_version(*)
(1.5ms) CREATE TABLE "users" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
ActiveRecord::Base.connection.tables => ["users"]
User.table_exists? => false
exit
in terminal/bash
rails console
in rails console
User.table_exists? => true
using:
Ruby 2.5.0
Rails 5.2.1
ActiveRecord::ModelSchema::ClassMethods#table_exists? is a wrapper around ActiveRecord::ConnectionAdapters::SchemaCache#data_source_exists?.
As the name implies, this is a cache, probably populated when the Rails app starts. If you modify the database schema the old schema will still be cached (arguably a bug or missing feature). You can reload User's schema cache with User.reload_schema_from_cache or possibly connection.schema_cache.clear_data_source_cache(Users.table_name.
I guess it's a cache thing. I couldn't tell if a bug or not, I don't have much knowledge of rails internals but I gues it's that way by design. It's not even a common use case to create tables from within the console.
You could skip the exit > enter steps calling reload! on the console to reload everything though.

Rails runs commands twice

When running my rails 4 application locally, every GET, PUT, etc. appears to run twice. I've cleaned the assets, thinking that this is causing the issue, but it does not appear to be the case. Interestingly, even commands run from the console appear to be run twice. For example, this is the output I get when asking about my users:
2.0.0-p247 :005 > User.all
User Load (0.7ms) SELECT "users".* FROM "users"
User Load (0.7ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 1, ...
Any ideas as to what's going on would be greatly appreciated.
I think its an open issue in Rails 4,
View this issue on github, thats exactly your case.
try this work around,
config.paths["log"] = '/dev/null'

Rails Active Record .all method deletes all records saved

I cannot understand what happened, but it seems that after all call .all method my records in one specific DB table disappear. If I run rake db:seed, table fills up. But once I call .all records disappear.
rake db:seed
After it I can see all records in UI.
rails c
Loading development environment (Rails 4.0.0)
2.0.0-p247 :001 > reload!
Reloading...
=> true
Here I still see records.
2.0.0-p247 :002 > Url.all
=> #<ActiveRecord::Relation []>
Here they disappear from DB and UI
It is the same for manual record creating via UI and console.
How could it be? How to fix it?
Try changing your model name from Url to something else. You can still use the same table name (urls) by using set_table_name 'urls'.

rails create error

I have a threaded Post using Ancestry.
When replying to a Post from another user I get :
on the line :
#post = current_user.posts.new(params[:post])
Started POST "/posts.js" for 127.0.0.1
at Tue Jun 07 13:50:19 +0300 2011
Processing by PostsController#create
as JS Parameters: {"commit"=>"Post",
"post"=>{"body"=>"a",
"parent_id"=>"5",
"discussion_id"=>"1"},
"authenticity_token"=>"RUra0Ndv67cgaGshBS5yCJMq5V6WG6OuZiqDbbWP5cc=",
"utf8"=>"✓"} User Load (0.2ms)
SELECT "users".* FROM "users" WHERE
("users"."id" = 33) LIMIT 1 Post
Load (0.2ms) SELECT "posts".* FROM
"posts" WHERE ("posts".user_id = 33)
AND ("posts"."id" = 5) LIMIT 1
Completed in 238ms
ActiveRecord::RecordNotFound (Couldn't
find Post with ID=5 [WHERE
("posts".user_id = 33)]):
app/controllers/posts_controller.rb:28:in
`create'
How can I debug it ?
My first pass would be to open rails console (at the prompt type 'rails console') and then try the following:
>> x = Post.find(5)
If you find it check the user_id on that record. Does it actually exist? If so, that's good to know.
Next I would take the SQL in your output above and run it manually in whatever db you use. If you're using SQLite3 you can do:
>> sqlite3 db/development.sqlite3
If it exists then it's a fair point to scratch your head. I suspect you will find that it's not there.
Is the link to reply to the post manually created in your view? Are you certain it's being built correctly--the two ids of interest are indeed what you intended them to be?
If it's correctly built then I would simply use the ruby debugger in your controller and begin stepping through code. If you're not familiar with the ruby debugger, you can get the gem as described in your Gemfile and then once it's in your gemset you can add this line of code where you want to breakpoint your code:
require 'ruby-debug'; debugger
Then you're free to explore as necessary.

Resources