Mongomapper is referencing db using name causing ReferenceError issue - ruby-on-rails-3

This is the query generated by Mongomapper:
MONGODB mydatabase['users'].find({:name=>"bob"}).limit(-1)
But this is not valid in the mongo console since the correct syntax is
db.users.find({:name=>"bob"}).limit(-1)
If I just use the generated one, I got this error in the console
Thu Jan 12 03:01:23 ReferenceError: mydatabase is not defined (shell):1
Is there any way to make it correct? This causes my rails application broken.

You can't use symbols in the MongoDB console as they are ruby and not javascript :-) Try this:
db.users.find({name: "bob"}).limit(-1)

It is not mongodb's issue. 406 is pretty much relating to the controller call.
I need to use:
render :json => #user
rather than
respond_to

Related

undefined method 'path' for nil:NilClass using chargify_api_ares gem

I feel like this should be a simple problem, but I'm pulling my hair out trying to track it down. I'm installed the chargify_api_ares gem, but can't do even basic things such as
Chargify::Subscription.create
As I get this path error. I feel like this must be a gem issue somehow but don't know where to go from here.
UPDATE: bundle show chargify_api_ares shows the correct path, I just somehow can't access it. Still trying random environment related things.
Looks like this is the source of the problem, in active_resource\base.rb:
# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>)
# This method is regenerated at runtime based on what the \prefix is set to.
def prefix(options={})
default = site.path
default << '/' unless default[-1..-1] == '/'
# generate the actual method based on the current site path
self.prefix = default
prefix(options)
end
As I understand it, Chargify.subdomain should be setting the site.path, but I don't understand activeresource well enough yet to know what's happening and will continue to dig.
I too had the same issue.
I executed the following on the console
Chargify.configure do |c|
c.api_key = "<<api_key>>"
c.subdomain = "<<subdomain>>"
end
After that performing any Chargify console commands went through fine.

SalesForce databasedotcom gem conflict with ActiveRecord

I encountered strange problem - when Im trying to get User information from SalesForce using databasedotcom gem like this:
owner = client.find("User", deal_from_sf.OwnerId)
I get ActiveRecord error ActiveRecord::RecordNotFound for User, id:0013000000XXXXX
How can I use this method without patching native gem (as I understand alias for find method will help)?
The answer is so simple - read the documentation!!!
The problem obviously was in the namespace which was Global by default and User treated like ActiveRecord model. But one should add just one line to salesforce.yml file:
sobject_module : YourModuleName
and specify module where your salesForce logic lives)
http://rubydoc.info/github/heroku/databasedotcom/master/Databasedotcom/Client#sobject_module-instance_method

invalid byte sequence in UTF-8 on page request

I'm getting "invalid byte sequence in UTF-8" on page requests (permalinks) and I have no idea why nor can I reproduce it but I do get a lot of exceptions like this:
A ArgumentError occurred in products#index:
invalid byte sequence in UTF-8
activesupport (3.0.4) lib/active_support/core_ext/object/blank.rb:68:in `=~'
-------------------------------
Request:
-------------------------------
* URL : http://www.mysite.com/category/category-name-\x8E~ice
* Parameters: {"page"=>1, "controller"=>"products", "action"=>"index", "category"=>"category-name-\x8E~ice"}
The string at the end should not be there ("-\x8E~ice"). Any idea why that shows up or what can I do to debug/reproduce it ?
Thanks
we created a rails middleware that filters out all the strange encodings that can not be handled within our app.
the problem that we encounter is that there are requests that have strange encodings, for example Cp1252 / Windows-1252. when ruby 1.9 tries to match those strings against utf-8 regexps it blows up.
i tried various ways of dealing with this problem by using iconv, but it looks like solutions that work on my mac don't work on the servers. so the simplest approach is probably the best...
I've just posted a new gem called UTF8Cleaner which is heavily based on #phoet and #pithyless' work. It include a Railtie, so you can just drop it in to your Gemfile and forget about those "invalid byte sequence" errors.
https://github.com/singlebrook/utf8-cleaner
Similar to #phoet, I also used a Rails Middleware to solve similar encoding issues.
Tested on Ruby 1.9.3 (no Iconv):
https://gist.github.com/3639014
If you are using apache (and mod_rails) you can prevent these invalid url requests from hitting your Rails application completely by following this answer:
https://stackoverflow.com/questions/13512727/how-can-i-configure-apache-to-respond-400-when-request-contains-an-invalid-byte/13527812#13527812

Ruby on Rails - Suppress an error message

I am using Rails 3 and AJAX and have a parent object which is being created through and AJAX request. This parent is sent with children and saves them all. However, if a child has an error, Rails will stop the request. Is there any way to tell Rails to ignore this? I understand the proper thing to do is find the problem within the Javascript sending the request and fix it. However, for the sake of learning, is there a way to tell Rails that some errors might be ignorable?
To save without validating use:
#parent.save(:validate => false)
Also, don't forget you can create conditional validation rules if needs be. For example, add a virtual attribute (an instance variable that is not persisted to the DB) accessible via bare_bones?. Then modify a validator like so:
validates_presence_of :nickname, :unless => "bare_bones?"
Then, in your controller you would do something like this:
#parent = Parent.new params[:parent]
#parent.bare_bones = true
#parent.save()
Hope this helps.
You are looking for exception handling.
begin
#code that may contain errors
rescue
#what to do if an error is encountered
end

How can I see the SQL ActiveRecord generates?

I'd like to check a few queries generated by ActiveRecord, but I don't need to actually run them. Is there a way to get at the query before it returns its result?
Both of these articles should help you do what you want.
http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing
http://weblog.jamisbuck.org/2007/1/31/more-on-watching-activerecord
i think it's buried in:
construct_finder_sql,
http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/38c492e3939dd9bf/?pli=1
tail -f log/development.log
Works in default settings or when you set your logger level to DEBUG.
Jamis' article is outdated, or at least doesn't work my Rails app (possibly due to some other reason with a 3 year old 30,000 line app). However this works in a console any time:
ActiveRecord::Base.connection.instance_variable_set :#logger, Logger.new(STDOUT)