Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as_json chokes on invalid data #51

Open
pr1001 opened this issue Aug 2, 2012 · 0 comments
Open

as_json chokes on invalid data #51

pr1001 opened this issue Aug 2, 2012 · 0 comments

Comments

@pr1001
Copy link

pr1001 commented Aug 2, 2012

While that doesn't exactly sound like a bad thing – it is being presented invalid data, after all! – I found the best way for one bad attribute not to take down everything was to monkeypatch the Json module:

module SimpleRecord::Json
  def as_json(options={})
    result = {
        'id' => self.id
    }
    result['json_class'] = self.class.name unless options && options[:exclude_json_class]
    defined_attributes_local.each_pair do |name, val|
      if val.type == :belongs_to
        result[name.to_s + "_id"] = get_attribute_sdb(name) rescue nil
      else
        result[name] = get_attribute(name) rescue nil
      end
    end
    result
  end
end

Perhaps this could be an option or just mentioned in the README if nothing else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant