Rails runs commands twice - ruby-on-rails

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'

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.

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

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.

Smart way to disable/enable logs on demand in Rails

I'm running a Rails app (v 3.1.10) on a Heroku Cedar stack with Papertrail add-on going crazy because of the size of the logs.
My app is really verbose and the logs are getting huge (really huge):
Sometimes because I serialize a lots of data in one field and that makes a huge SQL request. In my model I have many:
serialize :a_game_data, Hash
serialize :another_game_data, Hash
serialize :a_big_set_of_game_data, Hash
[...]
Thanks to my AS3 Flash app working with bigs sets of json...
Sometimes because there's a lots of partials to render:
Rendered shared/_flash_message.html.erb (0.1ms)
Rendered shared/_header_cart_info.html.erb (2.7ms)
Rendered layouts/_header.html.erb (19.4ms)
[...]
It's not the big issue here, but I've added this case too because Jamiew handle it, see below...
Sometimes because there's lots of sql queries on the same page:
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Course Load (5.3ms) SELECT "courses".* FROM "courses" WHERE (id = '1' OR pass_token = NULL)
Session Load (1.3ms) SELECT "sessions".* FROM "sessions" WHERE "sessions"."id" = 1 LIMIT 1
Training Load (1.3ms) SELECT "trainings".* FROM "trainings" WHERE "trainings"."id" = 1 LIMIT 1
[...]
It's a big (too) complex App we've got here... yeah...
Sometimes because there's a lots of params:
Parameters: {"_myapp_session"=>"BkkiJTBhYWI1MUVlaVdtbE9Eb1Y2I5BjsAVEkiEF9jc3JmX3Rva2VlYVWZyM2I0dEZaR1YwNXFjZhZTQ1uBjsARkkiUkiD3Nlc3Npb25faWQGOgZFRhcmRlbi51c2yN1poVm8vdWo3YTlrdUZzVTA9BjsARkkiH3dAh7CMTQ0Yzc4ZDJmYzg5ZjZjOGQ5NVyLmFkbWluX3VzZXIua2V5BjsAVFsISSIOQWRtaW5Vc2VyBjsARlsGaQZJIiIkMmEkMTAkcmgvQ2Rwc0lrYzFEbGJFRG9jMnZvdQY7AFRJIhl3YXJkZW4udXNlci51c2VyLmtleQY7AFRbCEkiCVVzZXIGOwBGWwZpBkkiIiQyYSQxMCRBUFBST2w0aWYxQmhHUVd0b0V5TjFPBjsAVA==--e4b53a73f6b622cfe7550b2ee12678712e2973c7", "authenticity_token"=>"EeiWmlODoYXUfr3b4tFZGV05qr7ZhVo/uj7a9kuFsU0=", "utf8"=>"✓", "locale"=>"fr", "id"=>"1", "a"=>1, "a"=>1, "a"=>1, "a"=>1, "a"=>1, "a"=>1, [...] Hey! You've reach the end of the line but it's not the end of the parameters...}
The AS3 Flash app send big json data to the controller...
I didn't mention the (in)famous "Assets pipeline logging problem" because now I'm using the quiet_assets gem to handle this:
https://github.com/evrone/quiet_assets
So... what did I try?
1: Dennis Reimann's middleware solution:
http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/
2: Spagalocco's gem (inspired by solution #1):
https://github.com/spagalloco/silencer
3: jamiew's monkeypatches (inspired by solution #1 + a bonus):
https://gist.github.com/1558325
Nothing is really working as expected but it's getting close.
I would rather use a method in my ApplicationController like this:
def custom_logging(opts={}, show_logs=true)
disable_logging unless show_logs
remove_sql_requests_from_logs if opts[:remove_sql_requests]
remove_rendered_from_logs if opts[:remove_rendered]
remove_params_from_logs if opts[:remove_params]
[...]
end
...and call it in any controller method: custom_logging({:remove_sql_requests=>1, :remove_rendered=>1})
You got the idea.
So, is there any good resource online to handle this?
Many thanks for your advices...
I"m the author of the silencer gem mentioned above. Are you looking to filter logging in general or for a particular action? The silencer gem handles the latter problem. While you can certainly use it in different ways, it's mostly intended for particular actions.
It sounds like what you are looking for less verbose logging. I would recommend you take a look at lograge. I use that in production in most of my Rails apps and have found it to be quite useful.
If you need something more specialized, you may want to look at implementing your own LogSubscriber which is essentially the lograge solution.
Set your log level in the Heroku enviroment
View your current log level:
heroku config
You most likely have "Info", which is just a lot of noise
Change it to warn or error
heroku config:add LOG_LEVEL=WARN
Also, when viewing the logs, only specify the "app" server
heroku logs --source app
I personally, append --tail to see the logs live.
heroku logs --source app --tail

Cucumber, Capybara & Rails 2.3.2 - FactoryGirl not committing records to database, but works from console?

I have a strange problem, when I run my (only / first) cucumber test, part of which creates a new entry in my Countries table using:
Factory.create(:country)
the models don't get committed to my database (MySql 5) & my test fails as the view tries to load this data. Here is a snippet from my test.log
[4;36;1mSQL (0.1ms)[0m [0;1mSAVEPOINT active_record_1[0m
[4;35;1mCountry Create (0.1ms)[0m [0mINSERT INTO `countries` (`name`, `country_code`, `currency_code`) VALUES('Ireland', 'IE', 'EUR')[0m
[4;36;1mSQL (0.1ms)[0m [0;1mRELEASE SAVEPOINT active_record_1[0m
[4;35;1mSQL (0.1ms)[0m [0mSAVEPOINT active_record_1[0m
However when I load up the rails console and run exactly the same command i.e. Factory.create(:country), the records get committed to the database. Here is the output from test.log
[4;36;1mSQL (3.5ms)[0m [0;1mBEGIN[0m
[4;35;1mCountry Create (0.2ms)[0m [0mINSERT INTO `countries` (`name`, `country_code`, `currency_code`) VALUES('Ireland', 'IE', 'EUR')[0m
[4;36;1mSQL (1.1ms)[0m [0;1mCOMMIT[0m
From env.rb
Cucumber::Rails::World.use_transactional_fixtures = false
Any advise is very much appreciated, I've spent the last two days trying to figure this out but had no success.
The second line of what you posted from your test.log file shows the record being inserted. I see that you've turned off transactional fixtures, which is fine.
Are you using the database_cleaner gem by chance? If so, it's going to essentially roll your database back to its original state after the tests are done. This means that, unless you pause your tests while they're running, and after data is inserted, you'll never see it in the DB because it gets removed after the test suite is run.
I don't know that this is what's causing the issue at the root of the thing, but it would definitely explain why, when you run just that one command from the console, that you see it in the DB, but you don't after you run your tests. The test data is supposed to be removed after the test suite executes, so your tests have a fresh, consistent starting point for each run. It's an important part of making sure that your tests run in the same environment every time, and therefore can be counted upon to give you reliable results.

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