{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1014","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1014/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1014/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1014/events","html_url":"https://github.com/tgriesser/bookshelf/issues/1014","id":115736692,"number":1014,"title":"Attributes are not transferred intact to format","user":{"login":"codeclown","id":211543,"avatar_url":"https://avatars.githubusercontent.com/u/211543?v=3","gravatar_id":"","url":"https://api.github.com/users/codeclown","html_url":"https://github.com/codeclown","followers_url":"https://api.github.com/users/codeclown/followers","following_url":"https://api.github.com/users/codeclown/following{/other_user}","gists_url":"https://api.github.com/users/codeclown/gists{/gist_id}","starred_url":"https://api.github.com/users/codeclown/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codeclown/subscriptions","organizations_url":"https://api.github.com/users/codeclown/orgs","repos_url":"https://api.github.com/users/codeclown/repos","events_url":"https://api.github.com/users/codeclown/events{/privacy}","received_events_url":"https://api.github.com/users/codeclown/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-11-08T14:26:02Z","updated_at":"2015-11-08T17:16:44Z","closed_at":null,"body":"In short, here's the issue illustrated (date is saved incorrectly):\r\n\r\n```\r\n(organization, subscriptionObject) => {\r\n    console.log('initial', moment.unix(subscriptionObject.current_period_end).format('YYYY-MM-DD'));\r\n    // => initial 2015-11-15\r\n\r\n    return organization\r\n        .save({\r\n            plan_active_until: moment.unix(subscriptionObject.current_period_end)\r\n        })\r\n        .then(organization => {\r\n            console.log('after save', organization.get('plan_active_until').format('YYYY-MM-DD'));\r\n            // => after save 2015-11-08\r\n        });\r\n}\r\n```\r\n\r\nI'm using a custom `format` function on the models, but it doesn't seem to be causing the issue. It looks like this:\r\n\r\n```\r\nfunction(attributes) {\r\n    if(this.hasTimestamps) {\r\n        this.dateTimeFields.map(field => {\r\n            if(field == 'plan_active_until' && attributes[field]) {\r\n                console.log('format', attributes[field].format('YYYY-MM-DD'));\r\n                // => format 2015-11-08  <-  ???\r\n            }\r\n\r\n            if(typeof attributes[field] == 'undefined' || attributes[field] === null) {\r\n                return attributes[field] = null;\r\n            }\r\n\r\n            if(!moment.isMoment(attributes[field])) {\r\n                attributes[field] = moment.utc(attributes[field]);\r\n            }\r\n\r\n            return attributes[field] = attributes[field].format(datetimeFormat);\r\n        });\r\n    }\r\n\r\n    return attributes;\r\n}\r\n```\r\n\r\nWhat this illustrates is that somehow the moment-instance changes before it is passed on to `format`.\r\n\r\nIs there something I'm misunderstanding here or is this a bug? I tried to create a testcase but had difficulties setting up the test environment."},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/154842106","html_url":"https://github.com/tgriesser/bookshelf/issues/1014#issuecomment-154842106","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1014","id":154842106,"user":{"login":"codeclown","id":211543,"avatar_url":"https://avatars.githubusercontent.com/u/211543?v=3","gravatar_id":"","url":"https://api.github.com/users/codeclown","html_url":"https://github.com/codeclown","followers_url":"https://api.github.com/users/codeclown/followers","following_url":"https://api.github.com/users/codeclown/following{/other_user}","gists_url":"https://api.github.com/users/codeclown/gists{/gist_id}","starred_url":"https://api.github.com/users/codeclown/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codeclown/subscriptions","organizations_url":"https://api.github.com/users/codeclown/orgs","repos_url":"https://api.github.com/users/codeclown/repos","events_url":"https://api.github.com/users/codeclown/events{/privacy}","received_events_url":"https://api.github.com/users/codeclown/received_events","type":"User","site_admin":false},"created_at":"2015-11-08T17:16:44Z","updated_at":"2015-11-08T17:16:44Z","body":"No, here's a simplified model definition:\r\n\r\n```\r\nlet dateTimeFields = ['created_at', 'updated_at'];\r\n\r\nlet Organization = baseModel.extend({\r\n    hasTimestamps: true,\r\n    dateTimeFields: [...dateTimeFields, 'plan_active_until']\r\n};\r\n```\r\n\r\nJust quickly tried basically moving the array to `hasTimestamps` and using it in `format` instead of `dateTimeFields`, with no luck."}}