User.count(field, :group => [:name], :conditions => {:active => true}) To get distinct count; pass “:distinct => true”
Posts Tagged ‘Ruby on Rails’
Rails – Group and count
Posted in Ruby on Rails, tagged Ruby on Rails on October 1, 2011 | Leave a Comment »
Rails – Creating a custom primary key and foreign key
Posted in Ruby on Rails, tagged Ruby on Rails on October 1, 2011 | Leave a Comment »
class User < ActiveRecord::Base has_one :account, :primary_key => :account_index, :foreign_key=> :account_index end class Account< ActiveRecord::Base belongs_to :user end
Import CSV Using Rails Migration
Posted in Ruby on Rails, tagged Ruby on Rails on September 30, 2011 | Leave a Comment »
require ‘csv’ CSV.foreach(“#{RAILS_ROOT}/db/seed/file.csv”, encoding: “UTF-8″ ,:row_sep => “\r\n”) do |row| field1,field2,field3 = row Foo.create(:field1 => field1, :field2 => field2, :field3 => field3) end
CSV parsing issues – MalformedCSVError, “Unquoted fields do not allow ” + “\\r or \\n
Posted in Ruby on Rails, Weird Errors, tagged bug fixes, Ruby on Rails on September 30, 2011 | Leave a Comment »
Here, you need to consider 2 things. CSV’s auto line ending detection may be failing for some reason. A likely cause could be fields that contain line endings different from those used to end lines. You should be able to solve this by setting the :row_sep manually. CSV.open(…, :row_sep => “\r\n”) If that doesn’t work, [...]
uninitialized constant CSV/FasterCSV
Posted in Ruby on Rails, Weird Errors, tagged bug fixes, Ruby on Rails on September 30, 2011 | Leave a Comment »
Ruby 1.9 has adopted FasterCSV as its built-in CSV library. However, it’s in the standard library rather than Ruby 1.9′s core, so you need to manually require it in the application. After adding a require ‘csv’ to your code, you can then do things such as CSV.parse(“this,is,my,data”) require ‘csv’ CSV.parse(“this,is,my,data”)
FasterCSV plus support for Ruby 1.9 issue fix
Posted in Ruby on Rails, Weird Errors, tagged bug fixes, Ruby on Rails on September 30, 2011 | Leave a Comment »
If you see the beow issue; Please switch to Ruby 1.9′s standard CSV library. It’s FasterCSV plus support for Ruby 1.9′s m17n encoding engine. Remove fasterCSV from your Gemfile in the application. Bundler is trying to require FasterCSV because you have it specified in the Gemfile.
Undo scaffolding in rails
Posted in Ruby on Rails, tagged Ruby on Rails on September 30, 2011 | Leave a Comment »
rails 3.0 or higher; rails generate scaffold <name> rails destroy scaffold <name> Also we can undo whatever we did with rails generate <something> By rails destroy <something>
Ruby: Sorting an array of objects by an attribute
Posted in Ruby on Rails, tagged ruby, Ruby on Rails on September 28, 2011 | Leave a Comment »
@users.sort! { |a,b| a.name.downcase <=> b.name.downcase } Since strings are comparable, this will sort user objects alphabetically by name. To sort on login_count; @users.sort! { |a,b| a.login_count <=> b.login_count }
Ruby on Rails – Multiple database connections
Posted in Ruby on Rails, tagged Ruby on Rails on September 24, 2011 | Leave a Comment »
Define the custom_databse details in database.yml. For example; custom_database: adapter: mysql username: root password: database: custom_development All we need to do is to tell which models to use which connection. (E.g) class Results < ActiveRecord::Base establish_connection :custom_database end
Installing Ruby on Rails, Thin, Nginx, PHP, SVN, Nokogiri and Rmagick/Imagemagick on Ubuntu – Step by step
Posted in installations, Ruby on Rails, tagged Ruby on Rails on September 24, 2011 | Leave a Comment »
Package versions may not be the latest one. These are the steps which I followed to set up my environment. You may want to download the latest source from web and then build it instead of using the download links mentioned here. sudo apt-get install build-essential sudo apt-get install openssl libssl-dev libmysqlclient-dev sudo apt-get install [...]