Skip to content

Conversation

@metaskills
Copy link
Member

@metaskills metaskills commented Mar 14, 2017

Adds a simple JSON type for creating an underlying nvarchar(max) column to add JSON too. Since SQL Server does not have a true JSON type. This means supporting JSON from a Rails perspective is a simple as adding the proper SQL Server type to your model's column which has some form of underlying string type.

create_table :users do |t|
  t.string :name, :email
  t.json :data # Creates a nvarchar(max) column.
 end

class Users < ActiveRecord::Base
  attribute :data, ActiveRecord::Type::SQLServer::Json.new
end

This allows you to save and return JSON data and query it as needed.

User.create! name: 'DJ Ruby Rhod', data: nil
User.create! name: 'Ken Collins', data: { 'admin' => true, 'foo' => 'bar' }

admin = User.where("JSON_VALUE(data, '$.admin') = CAST(1 AS BIT)").first
admin.data['foo'] # => "bar"

Fixes #485.

@metaskills metaskills merged commit 83f6d57 into master Mar 14, 2017
@meet-bhagdev
Copy link

@metaskills this is really cool. Would something similar work in Tiny_TDS?

@metaskills
Copy link
Member Author

Oh yea... TinyTDS is just the means to execute SQL. In reality this work is more about documentation than doing anything.

@metaskills metaskills deleted the kc/JSON branch May 29, 2017 00:50
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

Successfully merging this pull request may close these issues.

3 participants