Tag: ActiveRecord

How to use serialized object in Active Record using Rails?

Serializing Object Will help us to store multiple column values in single Database column and even making processing using that column simple. It helps to extended the table without adding new column in table. Because we can store multiple column values in single column as Hash value. So, It reduce the difficulty of retriving and …

Continue reading

Permanent link to this article: https://blog.openshell.in/2013/11/how-to-use-serialized-object-in-active-record-using-rails/

Connect MS SQL Server with Rails

To connect MS SQL Server with Ruby on Rails follow the below steps: SQL Server (2005 or higher recommended) Install the adapters and driver [code] gem install tiny_tds gem install activerecord-sqlserver-adapter [/code] Ensure the activerecord adapter and db driver gems are defined in your Gemfile [code] gem ‘tiny_tds’ gem ‘activerecord-sqlserver-adapter’ [/code]

Permanent link to this article: https://blog.openshell.in/2013/04/connect-ms-sql-server-with-rails/

How to automate whodidit behaviour and timestamps in yii ActiveRecord model?

To automate whodidit and timestamps in yii activerecord model using beforesave() method. If the rule validation got success then only the beforesave() method will invoke. Without satisifying rule() in activerecord model, this method will not work. [php] protected function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { $this->created_on = time(); $this->created_by = (int)Yii::app()->user->id; } $this->updated_on = time(); …

Continue reading

Permanent link to this article: https://blog.openshell.in/2013/03/how-to-automate-whodidit-behaviour-and-timestamps-in-yii-activerecord-model/

How to automate timestamps in activerecord using yii

There are many ways to automate the setting of timestamps in yii ActiveRecord models. In this we are representing three ways here. 1) Update via model’s rules: [php] /** * @return array validation rules for model attributes. */ public function rules() { return array( array(‘title’,’length’,’max’=>255), array(‘title, created_at, updated_at’, ‘required’), array(‘updated_at’,’default’, ‘value’=>new CDbExpression(‘NOW()’), ‘setOnEmpty’=>false,’on’=>’update’), array(‘created_at,updated_at’,’default’, ‘value’=>new …

Continue reading

Permanent link to this article: https://blog.openshell.in/2013/03/how-to-automate-timestamps-in-activerecord-using-yii/

Finders are great but be careful

Finders are very pleasant to use, enable you to write readable code and they don’t require in-depth SQL knowledge. But the nice high level abstraction come with a computational cost. Follow these rules of thumb: Retrieve only the information that you need. A lot of execution time can be wasted by running selects for data …

Continue reading

Permanent link to this article: https://blog.openshell.in/2010/11/finders-are-great-but-be-careful/

Group operations in a transaction

ActiveRecord wraps the creation or update of a record in a single transaction. Multiple inserts will then generate many transactions (one for each insert). Grouping multiple inserts in one single transaction will speed things up. Insead of: my_collection.each do |q| Report.create({:phrase => q}) end Use: Report.transaction do my_collection.each do |q| Report.create({:phrase => q}) end end …

Continue reading

Permanent link to this article: https://blog.openshell.in/2010/11/group-operations-in-a-transaction/