15 Jun 2013
Richard Huang
ActiveRecord provides default_scope to set a default scope for all operations on the model, it looks convenient at first, but will lead to some unexpected behaviors, we should avoid using it.
Read More
Tags
model
29 Sep 2012
Zamith
Methods should focus on what you want done and not how you want it.
Read More
Tags
model
refactor
02 May 2012
Richard Huang
Most developers use AR callbacks after_create/after_update/after_destroy to generate background job, expire cache, etc., but they don't realize these callbacks are still wrapped in database transaction, they probably got unexpected errors on production servers.
Read More
Tags
model
06 Mar 2012
Richard Huang
Rails mass assignment feature is really useful, but it may be a security issue, it allows an attacker to set any models' attributes you may not expect. To avoid this, we should add attr_accessbile or attr_protected to all models.
Read More
Tags
model
security
05 Dec 2011
Brasten Sager
Business model methods should be named after the logic / business value they provide, not the implementation details. Violations to this practice tend to show up on ActiveRecord models.
Read More
Tags
model
23 Jun 2011
Angelo Capilleri
Use select with has_many and belongs_to on Active Record Associations
Read More
Tags
refactor
model
performance
31 May 2011
Guo Lei
Make the app/models more clear using namespaced models
Read More
Tags
model
31 May 2011
Guo Lei
When the business logic of models becomes complex, it's very helpful to keep a consistent code structure that is agreed by team members.
Read More
Tags
model
23 Jan 2011
Richard Huang
Are you tired of going to schema.rb to find your table structures information? It would be better to list all the attributes of the model in the model itself.
Read More
Tags
model
gem
comment
03 Oct 2010
Richard Huang
Do you always check if ActiveRecord's attributes exist or not by nil?, blank? or present? ? Don't do that again, rails provides a cleaner way by query attribute
Read More
Tags
model
09 Sep 2010
Richard Huang
If you want to do a large data query such as finding all the 10,000,000 users to send email to them, you should use batched finder to avoid eating too much memory.
Read More
Tags
model
performance
task
23 Aug 2010
Richard Huang
I don't remember how many times I need to fetch current user in models, such as audit log. Here is a flexible way to set the current user in and fetch the current user from User model.
Read More
Tags
model
18 Aug 2010
Richard Huang
This is a flexible and reusable solution for multiple uploads, using STI model to save all the uploaded assets in one "assets" table and using polymorphic model to reuse "Asset" model in different uploadable models.
Read More
Tags
model
upload
25 Jul 2010
Wen-Tien Chang
N+1 Queries is a serious database performance problem. Be careful of that situation! If you're not sure, I recommend you install http://github.com/flyerhzm/bullet plugin, which helps you reduce the number of queries with alerts (and growl).
Read More
Tags
model
plugin
24 Jul 2010
Wen-Tien Chang
Observer serves as a connection point between models and some other subsystem whose functionality is used by some of other classes, such as email notification. It is loose coupling in contract with model callback.
Read More
Tags
model
observer
24 Jul 2010
Wen-Tien Chang
According to the law of demeter, a model should only talk to its immediate association, don't talk to the association's association and association's property, it is a case of loose coupling.
Read More
Tags
model
24 Jul 2010
Wen-Tien Chang
According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into model.
Read More
Tags
model
view
24 Jul 2010
Wen-Tien Chang
If a model has some related columns, e.g. a user has an address_city and an address_street, you can extract these properties into a composed class.
Read More
Tags
model
24 Jul 2010
Wen-Tien Chang
Some codes in your model are related, they are take charge of the same things, such as logging and authorization. We can extract these codes into a module to reuse them.
Read More
Tags
model
24 Jul 2010
Wen-Tien Chang
If you find some methods whose definitions are similar, only different by the method name, it may use meta programming to simplify the things.
Read More
Tags
model
23 Jul 2010
Wen-Tien Chang
named_scope is awesome, it makes your codes much more readable, you can also combine named_scope finders to do complex finders.
Read More
Tags
rails2
controller
model
23 Jul 2010
Wen-Tien Chang
According to the decoupling principle, model should do finders by itself, a model should not know too much about associations finders logic.
Read More
Tags
rails2
model
22 Jul 2010
Wen-Tien Chang
Use accepts_nested_attributes_for to make nested model forms much easier, this feature is provided by rails 2.3
Read More
Tags
controller
model
view
22 Jul 2010
Wen-Tien Chang
When you want to associate a model to many association models by checkbox on view, you should take advantage of model.collection_model_ids to reduce the code in controller.
Read More
Tags
controller
model
view
21 Jul 2010
Wen-Tien Chang
Use model callback can avoid writing some logic codes in controller before or after creating, updating and destroying a model.
Read More
Tags
controller
model
21 Jul 2010
Wen-Tien Chang
Sometimes you will build a complex model with params, current_user and other logics in controller, but it makes your controller too big, you should move them into model with a factory method
Read More
Tags
controller
model
21 Jul 2010
Wen-Tien Chang
In MVC model, controller should be simple, the business logic is model's responsibility. So we should move logic from controller into the model.
Read More
Tags
controller
model
21 Jul 2010
Wen-Tien Chang
Do not assign the model's attributes directly in controller. Add model virtual attribute to move the assignment to model.
Read More
Tags
controller
model
19 Jul 2010
Wen-Tien Chang
Use model association to avoid assigning reference in controller.
Read More
Tags
controller
model
14 Jul 2010
Wen-Tien Chang
Complex finders in controller make application hard to maintain. Move them into the model as named_scope can make the controller simple and the complex find logics are all in models.
Read More
Tags
rails2
controller
model