Question: I have a situation where a parent `Order` has multiple child `Items`. Both `Order` and `Item` have a `status_id` column. I want the user to update the `status_id` of the `Item`, and then when all of the `Items` have a `status_id`, then the `Order's status_id` should be auto-set (to some value based on what `Item status_ids` are). 

The code that I currently have is this:

```
class Item
  after_save :set_order_status_id

  def set_order_status_id
    if self.order.items.where(status_id:nil).blank?
       self.order.update_attributes(status_id:X)
    end
  end
end

```

Ths is pretty smelly code because it violates SRP, uses a callback, and is pretty inefficient, considering that this means if an `Order` has 5 `Items` and all 5 `Items` are being updated, after EACH `Item` update, the `set_order_status_id` method is called, and a database query is run. 

So... is there a better way of writing this to avoid these issues? Particularly I'm interested in removing the inefficiency with constantly checking the parent `Order's` other child `Items'` statuses... because again if an all 5 `Items` is updated at once, it's silly to check after each and every update when it should just wait until the 5th update.... Does Rails have a magical way of doing this?

Answer: 
First, the monad law assumes `f: A => M[A]` (here `f: A => List[A]`). This is not true of `(x: Int) => Some[Int](x)`.

Second, `List`'s `flatMap` is *not* monadic bind. It is more general than bind, because it takes an implicit `CanBuildFrom` that allows it to change its return type depending on what you want it to return. You can restrict it to bind like so

```
def bind[A](xs: List[A])(f: A => List[A]) = xs.flatMap(f) // implicit (List.canBuildFrom)

```

Now you can see that the law is satisfied:

```
bind(List(1))(x => List(x, x)) == List(1, 1)

```
I'm not a category theory or Haskell expert but I don't understand your question, and my response is too big for comment, not to mention code blocks look terrible in comment.

Haskell left identity law is `return a >>= f ≡ f a`, right?

In Scala:

```
return -> apply
>>=    -> flatMap

```

So, left identity law for Scala `List` would be `List(a).flatMap(f) = f(a)`

In your case, `val a = 1` and `val f = (x: Int) => Some[Int](x)`. But this wouldn't even compile because `Option` is not a `GenTraversableOnce`; you can't return `Option` from a `List.flatMap`.

Instead, if we define `val f = (x: Int) => List(x * 2)`, a double function

```
LHS: List(a).flatMap(f) = List(2)
RHS: f(a) = List(2)

```

LHS = RHS, left identity satisfied.

Am I missing something?