class RailsBestPractices::Reviews::UseModelAssociationReview
then model association should be used instead of xxx_id assignment.
and the receivers of attribute assignment node and call node are the same,
and after it, there is a call node with message “save” or “save!”,
if there is an attribute assignment node with message xxx_id=,
check model define nodes in all controller files,
Review process:
Implementation:
See the best practice details here rails-bestpractices.com/posts/2010/07/19/use-model-association/
review a controller file to make sure to use model association instead of foreign key id assignment.
def attribute_assignment(node)
check an attribute assignment node, if its message is xxx_id,
def attribute_assignment(node) if node.left_value.message.is_a?(Sexp) && node.left_value.message.to_s =~ /_id$/ receiver = node.left_value.receiver.to_s @assignments[receiver] = true end end
def call_assignment(node)
if the receiver of call node exists in @assignments,
check a call node with message "save" or "save!",
def call_assignment(node) if ['save', 'save!'].include? node.message.to_s receiver = node.receiver.to_s add_error "use model association (for #{receiver})" if @assignments[receiver] end end