{"action":"created","issue":{"url":"https://api.github.com/repos/lotus/model/issues/138","labels_url":"https://api.github.com/repos/lotus/model/issues/138/labels{/name}","comments_url":"https://api.github.com/repos/lotus/model/issues/138/comments","events_url":"https://api.github.com/repos/lotus/model/issues/138/events","html_url":"https://github.com/lotus/model/issues/138","id":53712537,"number":138,"title":"Attributes not coerced when persisted","user":{"login":"angeloashmore","id":8601064,"avatar_url":"https://avatars.githubusercontent.com/u/8601064?v=3","gravatar_id":"","url":"https://api.github.com/users/angeloashmore","html_url":"https://github.com/angeloashmore","followers_url":"https://api.github.com/users/angeloashmore/followers","following_url":"https://api.github.com/users/angeloashmore/following{/other_user}","gists_url":"https://api.github.com/users/angeloashmore/gists{/gist_id}","starred_url":"https://api.github.com/users/angeloashmore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/angeloashmore/subscriptions","organizations_url":"https://api.github.com/users/angeloashmore/orgs","repos_url":"https://api.github.com/users/angeloashmore/repos","events_url":"https://api.github.com/users/angeloashmore/events{/privacy}","received_events_url":"https://api.github.com/users/angeloashmore/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-08T04:04:35Z","updated_at":"2015-01-08T12:06:53Z","closed_at":null,"body":"```ruby\r\nclass User\r\n  include Lotus::Entity\r\n  attributes :id, :name, :age\r\nend\r\n\r\nmapper = Lotus::Model::Mapper.new do\r\n  collection :users do\r\n    entity User\r\n\r\n    attribute :id,   String\r\n    attribute :name, String\r\n    attribute :age,  Integer\r\n  end\r\nend.load!\r\n\r\nuser1 = User.new(name: 'Luca', age: '32')\r\nUserRepository.persist(user1)\r\n```\r\n\r\nWhen `user1` is persisted, the age is **not** coerced to an `Integer` before persisting and remains as a string. For dynamic datastores (e.g. RethinkDB), the type is kept intact.\r\n\r\nWhen retrieved from storage, `user1.age` is coerced to an `Integer` from a `String`.\r\n\r\nIs this behavior intended? I can see how input should be type-validated before it reaches the persisting method, but defining the type in the mapper should guarantee the type is saved properly.\r\n\r\nIs it the job of the adapter to coerce the attributes?"},"comment":{"url":"https://api.github.com/repos/lotus/model/issues/comments/69171059","html_url":"https://github.com/lotus/model/issues/138#issuecomment-69171059","issue_url":"https://api.github.com/repos/lotus/model/issues/138","id":69171059,"user":{"login":"joneslee85","id":135605,"avatar_url":"https://avatars.githubusercontent.com/u/135605?v=3","gravatar_id":"","url":"https://api.github.com/users/joneslee85","html_url":"https://github.com/joneslee85","followers_url":"https://api.github.com/users/joneslee85/followers","following_url":"https://api.github.com/users/joneslee85/following{/other_user}","gists_url":"https://api.github.com/users/joneslee85/gists{/gist_id}","starred_url":"https://api.github.com/users/joneslee85/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joneslee85/subscriptions","organizations_url":"https://api.github.com/users/joneslee85/orgs","repos_url":"https://api.github.com/users/joneslee85/repos","events_url":"https://api.github.com/users/joneslee85/events{/privacy}","received_events_url":"https://api.github.com/users/joneslee85/received_events","type":"User","site_admin":false},"created_at":"2015-01-08T12:06:53Z","updated_at":"2015-01-08T12:06:53Z","body":"The coercion is the job of the Repository. After the repository persist the entity, it does not refresh the entity thus you might see the entity's values are not coerced.\r\n\r\n If you look into the DB, the stored values are coerced. And when you use Repository to find the entity, this time the entity values would reflect correctly.\r\n\r\nI think it is a good idea to refresh the states of the entity after successful persist operation. Thoughts?"}}