How to query MongoDB directly from Ruby instead of using Mongoid?

If you’re using Mongoid 3, it provides easy access to its MongoDB driver: Moped. Here’s an example of accessing some raw data without using Models to access the data: db = Mongoid::Sessions.default # inserting a new document collection = db[:collection_name] collection.insert(name: ‘my new document’) # finding a document doc = collection.find(name: ‘my new document’).first # … Read more

How do I run multiple lines of Ruby in html.erb file

If you need extra functions in your view, you normally declare those inside a helper. For each controller, if there is a helper it is automatically loaded. For instance, if you have a PeopleController, in the app/helpers folder, there should be a people_helper.rb, and it should look like this module PeopleHelper def name #do something … Read more

Saving the images Dimensions (width and height) in Paperclip?

Just for the sake of completeness, even though previous answers already show good enough suggestions. You can utilize Paperclip event handlers instead of Rails callbacks. In this case, size will be recalculated only when image changes. (If you’re using S3 for storage, this can save quite some time) has_attached_file :image, :styles => … after_post_process :save_image_dimensions … Read more

Rails coding standards – Why 2 space indentation?

It’s a matter of convention. The really important thing is consistency. Most (but not all) developers prefer spaces to tabs because they look the same regardless of any particular text editor / ide setting. http://www.ecyrd.com/JSPWiki/wiki/WhyTabsAreEvil Two spaces over four is also a matter of convention. Ruby code aims to minimize extra characters, and I suppose … Read more

Rails 4.0 expire_fragment/cache expiration not working

I believe the issue is that when you cache the fragment in your view, a cache digest is being added to the cache key (views/all_available_releases/41cb0a928326986f35f41c52bb3d8352), but expire_fragment is not using the digest (views/all_available_releases). If you add skip_digest: true to the cache call in the view it should prevent the digest from being used. <% cache … Read more

Rails migration does not change schema.rb

From the documentation: The rake db:reset task will drop the database, recreate it and load the current schema into it. This is not the same as running all the migrations. It will only use the contents of the current schema.rb file. If a migration can’t be rolled back, ‘rake db:reset’ may not help you. To … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)