require_relative 'setup' # # Array - Since 8.4 # # An array of a defined type (string, integer, ...) # # http://www.postgresql.org/docs/9.4/static/arrays.html # class Post < ActiveRecord::Base end describe "array" do before(:all) do ActiveRecord::Schema.define do create_table :posts, force: true do |t| t.string :title t.string :tags, array: true end end Post.reset_column_information end before { Post.delete_all } let!(:tdd) { Post.create!(title: "TDD is dead", tags: ["tdd", "testing", "death", false]).reload } let!(:bdd) { Post.create!(title: "BDD is woot", tags: ["bdd", "testing", "woot", true]).reload } let!(:foo) { Post.create!(title: "FOO is bar", tags: ["foo", "bar", 42]).reload } it "keeps order and converts everything to strings (as it's an array of strings)" do expect(tdd.tags).to eq ["tdd", "testing", "death", "0"] expect(bdd.tags).to eq ["bdd", "testing", "woot", "1"] expect(foo.tags).to eq ["foo", "bar", "42"] end it "saves when an element is changed" do tdd.tags[1] = "testing" tdd.save! tdd.reload expect(tdd.tags[1]).to eq "testing" end it "searches for array containing a value" do expect(Post.where("'testing' = ANY (tags)").all).to eq [tdd, bdd] end it "searches for a value at a specific position (index starts at 1!)" do expect(Post.where("tags[2] = 'testing'").all).to eq [tdd, bdd] end end