Controlling the order of rails validations

I’m not sure it’s guaranteed what order these validations get run in, as it might depend on how the attributes hash itself ends up ordered. You may be better off making your validate method more resilient and simply not run if some of the required data is missing. For example: def within_required_range? return if ([ … Read more

Forbidden Attributes Error in Rails 4 when encountering a situation where one would have used attr_accessible in earlier versions of Rails

Rails 4 now has features from the strong_parameters gem built in by default. One no longer has to make calls :as => :admin, nor do you need the attr_accessible :user_attribute, :as => admin in your model. The reason for this is that, by default, rails apps now have ‘security’ for every attribute on models. You … Read more

How to implement multiple different serializers for same model using ActiveModel::Serializers?

You can have multiple serializers for the same model, e.g. class SimplePersonSerializer < ActiveModel::Serializer attributes :id, :name end and class CompletePersonSerializer < ActiveModel::Serializer attributes :id, :name, :phone, :email end simple info for people in one controller: render json: @people, each_serializer: SimplePersonSerializer complete info for people in another: render json: @people, each_serializer: CompletePersonSerializer simple info for … Read more

ActiveModel::MissingAttributeError occurs after deploying and then goes away after a while

You probably have a query that doesn’t return all the columns (i.e. uses :select) and then cache_money; or some other ActiveRecord plugin uses an after_initialize callback, which executes whenever a new ActiveRecord object is created (i.e. when fetched from the database). In that initialize callback, something tries to access or use an attribute that wasn’t … Read more

LoadError Unable to autoload constant Message

By convention in rails (and this is enforced by autoloader), file paths should match namespaces. So, if you have a Mailboxer::Message model, it should be in app/models/mailboxer/message.rb. Additionally, you probably have autoloader kicking in when trying to load a Message class (my guess is that it happens from within ActAsMessageable). It looks for a message.rb … Read more

Difference between Active Model, Active Record and Active Resource

Rails 3 is designed with modularity in mind. Each module has its own purpose and functionality. ActiveModel: This component was created in Rails 3. They took all the model related parts that did not have a database requirement of Rails 2 ActiveRecord and moved it into ActiveModel. So ActiveModel includes things like validations. More information: … Read more

Getting types of the attributes in an ActiveRecord object

In Rails 3, for your model “Driver”, you want Driver.columns_hash. Driver.columns_hash[“name”].type #returns :string If you want to iterate through them, you’d do something like this: Driver.columns_hash.each {|k,v| puts “#{k} => #{v.type}”} which will output the following: id => integer name => string created_at => datetime updated_at => datetime

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