Cloning a record in rails, is it possible to clone associations and deep copy?

You may get some good use out of the Amoeba gem for ActiveRecord 3.2. It supports easy and automatic recursive duplication of has_one, has_many and has_and_belongs_to_many associations, field preprocessing and a highly flexible and powerful configuration DSL that can be applied both to the model and on the fly. be sure to check out the … Read more

Yii2 : Active Record add Not In condition

Well all query operands seems now merged within in yii\db\QueryInterface::Where() per documentation an In condition can now be added using something like $query = MyModel::find()->where([‘attribute’=>$array]); for a not In condition it is slightly different format $query = MyModel::find()->where([‘not in’,’attribute’,$array]);

Understanding how establish_connection works in ActiveRecord

AR calls establish_connection only once, for ActiveRecord::Base. All subclasses use the one connection. You can manually call establish connection yourself on some subclasses. This is very convenient for using two databases at once, e.g. class MyMainUser < ActiveRecord::Base; end class MyOtherDb < ActiveRecord::Base; end class MyOtherUser < MyOtherDb; end MyOtherDb.establish_connection … MyMainUser.first # uses default … Read more

Is there a way to make Rails ActiveRecord attributes private?

Jordini was most of the way there Most of active_record happens in method_missing. If you define the method up front, it won’t hit method_missing for that method, and use yours instead (effectively overwriting, but not really) class YourModel < ActiveRecord::Base private def my_private_attribute self[:my_private_attribute] end def my_private_attribute=(val) write_attribute :my_private_attribute, val end end

Rails first_or_create ActiveRecord method

From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set … Read more

What is the difference between attr_accessible(*attributes) & attr_protected(*attributes)?

attr_accessible (documentation) says “the specified attributes are accessible and all others are protected” (think of it as whitelisting.) whereas attr_protected (documentation) says “the specified attributes are protected and all others are accessible” (think of it as blacklisting.) A protected attribute is one that can only be modified explicitly (e.g. via attribute=) and can’t be updated … Read more

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