I have models order.rb , line\_items.rb -- where line\_item is belongs to order. I've added custom validations, as such **an order should only have line\_items all with same merchant\_id**

My question is : it seems the validation is working fine -- i am able to get the error whenever it is violated, but "line\_item" records are still saved in any situation. Any idea?

**Order.rb**

```
validate_on_update :only_one_merchant

def only_one_merchant
        merchants = []
        for line_item in self.line_items
          merchants << line_item.product.merchant_id
        end
        merchants = merchants.uniq
        errors.add(:line_items, "You could only order from one same merchant at time")  if  merchants.length > 1
    end 

```

**Order.rb** adding line\_item

```
  current_item = LineItem.new(:quantity => quantity)
  current_item.variant = variant
  current_item.price   = variant.price
  self.line_items << current_item

```