Validate the number of has_many items in Ruby on Rails
A better solution has been provided by @SooDesuNe on this SO post validates :tags, length: { minimum: 1, maximum: 6 }
A better solution has been provided by @SooDesuNe on this SO post validates :tags, length: { minimum: 1, maximum: 6 }
For this particular task, you only need to change the DB connection, so as Adam pointed out, you can do this: namespace :db do namespace :test do task :reset do ActiveRecord::Base.establish_connection(‘test’) Rake::Task[‘db:drop’].invoke Rake::Task[‘db:create’].invoke Rake::Task[‘db:migrate’].invoke ActiveRecord::Base.establish_connection(ENV[‘RAILS_ENV’]) #Make sure you don’t have side-effects! end end end If your task is more complicated, and you need other aspects … Read more
You can perform a query like this: Report.joins(:servers).where(:servers => {:company_id => 5}) To me, this is the cleaner solution to raw SQL.
For Rails 3, ActiveSupport::Callbacks gives you the necessary control. I was just facing the same challenge in a data integration scenario where normally-desirable-callbacks needed to be brushed aside. You can reset_callbacks en-masse, or use skip_callback to disable judiciously, like this: Vote.skip_callback(:save, :after, :add_points_to_user) ..after which you can operate on Vote instances with :add_points_to_user inhibited
You can raise an exception which you then catch. Rails wraps deletes in a transaction, which helps matters. For example: class Booking < ActiveRecord::Base has_many :booking_payments …. def destroy raise “Cannot delete booking with payments” unless booking_payments.count == 0 # … ok, go ahead and destroy super end end Alternatively you can use the before_destroy … Read more
I resolved it by doing following step. Step 1: go to Project_Root_Directory/config/environment/development.rb Change this line Rails.application.configure do To Your_Rails_Application_Folder_name::Application.configure do For example my rails project folder name is ‘Spree_demo’ so Your_Rails_Application_Folder_name in the following line: Your_Rails_Application_Folder_name::Application.configure do will be replaced as SpreeDemo::Application.configure do Note: See underscore in your application folder name it gets removed. Hope … Read more
id is just attr_protected, which is why you can’t use mass-assignment to set it. However, when setting it manually, it just works: o = SomeObject.new o.id = 8888 o.save! o.reload.id # => 8888 I’m not sure what the original motivation was, but I do this when converting ActiveHash models to ActiveRecord. ActiveHash allows you to … Read more
This command should do the trick (provided that you installed it using a dpkg-based packet manager): aptitude purge ruby
By default, Rails doesn’t add subfolders of the models directory to the autoload path. Which is why it can only find namespaced models — the namespace illuminates the subdirectory to look in. To add all subfolders of app/models to the autoload path, add the following to config/application.rb: config.autoload_paths += Dir[Rails.root.join(“app”, “models”, “{*/}”)] Or, if you … Read more
FasterCSV is definitely the way to go, but if you want to serve it directly from your Rails app, you’ll want to set up some response headers, too. I keep a method around to set up the filename and necessary headers: def render_csv(filename = nil) filename ||= params[:action] filename += ‘.csv’ if request.env[‘HTTP_USER_AGENT’] =~ /msie/i … Read more