rails create error - ruby-on-rails

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.

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.

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 save dangerous attribute srror

I'm getting the following error
i created a new scaffold bid with few fields
when i tried to access localhost:3000/bids/new
i get following error
ActiveRecord::DangerousAttributeError in BidsController#new
save is defined by ActiveRecord
in server side i see this result:
Started GET "/bids/new" for 127.0.0.1 at 2013-09-07 12:52:43 +0530
Processing by BidsController#new as HTML
Refinery::Role Load (0.4ms) SELECT "refinery_roles".* FROM "refinery_roles" WHERE "refinery_roles"."title" = 'Refinery' LIMIT 1
Refinery::User Load (0.3ms) SELECT "refinery_users".* FROM "refinery_users" INNER JOIN "refinery_roles_users" ON "refinery_users"."id" = "refinery_roles_users"."user_id" WHERE "refinery_roles_users"."role_id" = 1
Completed 500 Internal Server Error in 779ms
ActiveRecord::DangerousAttributeError (save is defined by ActiveRecord):
app/controllers/bids_controller.rb:27:in `new'
app/controllers/bids_controller.rb:27:in `new'
what am i missing or is refinerycms mounted at / is overriding functions?
please help me
It Seems, you have defined some reserved ActiveRecord's attributes, If you did so, than you either have to change those or try something else. Just add a gem in your gemfile and the gem will take care of name collisions automatically.
gem 'safe_attributes'
hope it will help. Thanks.
I have solved my problem,
The problem is not with refinery cms
i created a field with name save:boolean
i changed it to save_x:boolean and now it works fine

Rails: How to find out what query is being run in SQL when a page loads?

I want to know what queries are being run when a User is interacting with different pages on the website? I'd also like to know how long each query took. How and where can I see that?
There's a log folder in every rails application with a .log file for each of the environment you've run your application in. You'll see these kind of stats in it:
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-11-29 11:09:47 -0400
Served asset /jquery.js - 200 OK (3ms)
Started GET "/users/auth/google/callback?_method=post&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=id_res&openid.op_endpoint=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fud&openid.response_nonce=2012-10-26T19%3A29%3A52ZkvQlFeFr5Rk78g&openid.return_to=http%3A%2F%2Flocalhost%3A3000%2Fusers%2Fauth%2Fgoogle%2Fcallback%3F_method%3Dpost&openid.assoc_handle=AMlYA9VjNZ-QIIMe5bhvtPLBsAdm5xMltOa7MEwUoW4Opx9tXd_khhcS&openid.signed=op_endpoint%2Cclaimed_id%2Cidentity%2Creturn_to%2Cresponse_nonce%2Cassoc_handle%2Cns.ext1%2Cext1.mode%2Cext1.type.ext2%2Cext1.value.ext2%2Cext1.type.ext0%2Cext1.value.ext0%2Cext1.type.ext3%2Cext1.value.ext3&openid.sig=ehFAJ1m8nPces8%2Bj6Ud%2FicpuohY%3D&openid.identity=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItOawljL9RKBE7iUQHk94UhJQ-4sDOTawUfpNc&openid.claimed_id=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItOawljL9RKBE7iUQHk94UhJQ-4sDOTawUfpNc&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_response&openid.ext1.type.ext2=http%3A%2F%2Faxschema.org%2FnamePerson%2Ffirst&openid.ext1.value.ext2=Fernando&openid.ext1.type.ext0=http%3A%2F%2Faxschema.org%2Fcontact%2Femail&openid.ext1.value.ext0=fernando%40findhorsesforsale.net&openid.ext1.type.ext3=http%3A%2F%2Faxschema.org%2FnamePerson%2Flast&openid.ext1.value.ext3=Mendez" for 127.0.0.1 at 2012-10-26 15:29:52 -0400
Started GET "/run_dates/current_pdf_generation_status.js?pdf_generation_id=e3ef90b05844012f4fc2723c91dfe57c&_=1332637701414" for 127.0.0.1 at 2012-03-24 21:08:29 -0400
Processing by RunDatesController#current_pdf_generation_status as JS
Parameters: {"pdf_generation_id"=>"e3ef90b05844012f4fc2723c91dfe57c", "_"=>"1332637701414"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
RunDate Load (0.1ms) SELECT "run_dates".* FROM "run_dates" WHERE (pdf_generation_id = 'e3ef90b05844012f4fc2723c91dfe57c') LIMIT 1
Rendered run_dates/current_pdf_generation_status.js.erb (0.1ms)
Completed 200 OK in 4ms (Views: 2.0ms | ActiveRecord: 0.3ms)
.....
Have a look at development.log if you are in development mode, it will show you all the queries with the time taken to ran that query. For production, see production.log and for staging look at staging.log
You can delete that file rails will create it again if you want to look at specific queries. but i would suggest instead of deleting, when you are hitting the page, print something different like '-----------------' that will differentiate other queries from the queries of the page you want to see.
If you are the User, you can use some browser plugin (e.g. Firebug)
If you are not the user, you can check the log to see what queries are done.

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

Resources