Updating Hanami Entity

I just noticed that it is not possible to modify attributes on entities. Using hanami console with the bookshelf project I get the following

$ hanami c
2.5.1 :001 > repository = BookRepository.new
 => #<BookRepository relations=[:books]>
2.5.1 :002 > book = repository.create(title: 'TDD', author: 'Kent Beck')
 => #<Book:0x00007fb8e3bb7d00 @attributes={:id=>2, :title=>"TDD", :author=>"Kent Beck", :created_at=>2018-05-05 00:06:54 UTC, :updated_at=>2018-05-05 00:06:54 UTC}>
2.5.1 :003 > book.title
 => "TDD"
2.5.1 :004 > book.title = 'XP'
...
`method_missing'
NoMethodError (undefined method `title=' for #<Book:0x00007fb8e3bb7d00>)
2.5.1 :005 >

Coming from a Rails background this surprised me a lot.
Is this by design?
Am I missing something?

If this is how hanami works, then which are the recommended steps to get an entity from database, do some processing and save the changes to database?

I was thinking in the following…

book = r repository.find 1
book_2 = Book.new title: 'XP'
book = repository.update 1, book_2

Is this fine?

hey, I answered to this question in this SO page :slight_smile:

This is what I was looking for:

"hanami entity is an immutable data structure"

Thank you very much for your clarification.

1 Like