skip certain validation method in Model

Update your model to this class Car < ActiveRecord::Base # depending on how you deal with mass-assignment # protection in newer Rails versions, # you might want to uncomment this line # # attr_accessible :skip_method_2 attr_accessor :skip_method_2 validate :method_1, :method_3 validate :method_2, unless: :skip_method_2 private # encapsulation is cool, so we are cool # custom … Read more

Counter Cache for a column with conditions?

With regards to the conditions with counter_cache, I would read this blog post. The one thing you should do is add the following to the migration file: add_column :employees, :projects_count, :integer, :default => 0, :null => false Employee.reset_column_information Employee.all.each do |e| Employee.update_counters e.id, :projects_count => e.projects.length end So you current projects count can get migrated … Read more

Where to store sensitive data in public rails app?

TLDR: Use environment variables! I think @Bryce’s comment offers an answer, which I’ll just flush out. It seems one approach Heroku recommends is to use environment variables to store sensitive information (API key strings, database passwords). So survey your code and see in which you have sensitive data. Then create environment variables (in your .bashrc … Read more

How to implement Active Record inheritance in Ruby on Rails?

Rails supports Single Table Inheritance. From the AR docs: Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this: class Company < ActiveRecord::Base; end class Firm < Company; end class Client … Read more

How to use long id in Rails applications?

Credits to http://moeffju.net/blog/using-bigint-columns-in-rails-migrations class CreateDemo < ActiveRecord::Migration def self.up create_table :demo, :id => false do |t| t.integer :id, :limit => 8 end end end See the option :id => false which disables the automatic creation of the id field The t.integer :id, :limit => 8 line will produce a 64 bit integer field

tech