Fork me on GitHub

model

    default_scope is evil

    15 Jun 2013

    Richard Huang (flyerhzm@gmail.com)

    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 


    Tell, don't ask

    29 Sep 2012

    Zamith (zamith.28@gmail.com)

    Methods should focus on what you want done and not how you want it. Read More

    Tags 


    use after_commit

    02 May 2012

    Richard Huang (flyerhzm@gmail.com)

    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 


    Protect mass assignment

    06 Mar 2012

    Richard Huang (flyerhzm@gmail.com)

    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 


    Name your model methods after their behavior, not implementation.

    05 Dec 2011

    Brasten Sager (brasten@brasten.me)

    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 


    Active Record Query Interface Optimization

    23 Jun 2011

    Angelo Capilleri (capilleri@yahoo.com)

    Use select with has_many and belongs_to on Active Record Associations Read More

    Tags 


    Namespaced models

    31 May 2011

    Guo Lei (guolei9@gmail.com)

    Make the app/models more clear using namespaced models Read More

    Tags 


    Keep code struture in models consistent

    31 May 2011

    Guo Lei (guolei9@gmail.com)

    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 


    Annotate your models

    23 Jan 2011

    Richard Huang (flyerhzm@gmail.com)

    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 


    Substituting before_filter :load_object

    12 Oct 2010

    (mail@dennisbloete.de)

    Instead of loading an object with a before_filter you can use a more intelligent helper_method to get the main object for the controller context. Read More

    Tags 


    Use query attribute

    03 Oct 2010

    Richard Huang (flyerhzm@gmail.com)

    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 


    Use batched finder for large data query

    09 Sep 2010

    Richard Huang (flyerhzm@gmail.com)

    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 


    Fetch current user in models

    23 Aug 2010

    Richard Huang (flyerhzm@gmail.com)

    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 


    Use STI and polymorphic model for multiple uploads

    18 Aug 2010

    Richard Huang (flyerhzm@gmail.com)

    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 


    Fix N+1 Queries

    25 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Use Observer

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    the Law of Demeter

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Move code into model

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Extract to composed class

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Extract into Module

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    DRY Metaprogramming

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Love named_scope

    23 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Keep Finders on Their Own Model

    23 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Nested Model Forms

    22 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Use accepts_nested_attributes_for to make nested model forms much easier, this feature is provided by rails 2.3 Read More

    Tags 


    model.collection_model_ids (many-to-many)

    22 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Use model callback

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Use model callback can avoid writing some logic codes in controller before or after creating, updating and destroying a model. Read More

    Tags 


    Replace Complex Creation with Factory Method

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Move Model Logic into the Model

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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 


    Add model virtual attribute

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Do not assign the model's attributes directly in controller. Add model virtual attribute to move the assignment to model. Read More

    Tags 


    Use model association

    19 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Use model association to avoid assigning reference in controller. Read More

    Tags 


    Move finder to named_scope

    14 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    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