Question: I would like to get the id of an object after creating it, like so:

```
 post = Post.create_with_data(data)
 post_id = post.id #id of the post object just created in previous line

```

However, post\_id is `nil`. How do I achieve my goal? I don't want to use `Post.last`, because it might create data conflict when multiple users trying to create post at the same time.

Thank you.

Answer: 
In your `create_with_data` do you `save` the object? The `id` is nil until it's been saved.
```
class Post < ActiveRecord::Base
  ...
  def data=(data)
    # ... use data here
  end
end

Post.create(data: data)

```

It's much cleaner and it will call save for you! Your error is because create\_with\_data is not persisting the object (not saving, failing a validation when saving, etc.)