Skip to content

Instantly share code, notes, and snippets.

@jgn
Last active February 3, 2016 17:50
Show Gist options
  • Save jgn/10e426ac9c90bf530a3a to your computer and use it in GitHub Desktop.
Save jgn/10e426ac9c90bf530a3a to your computer and use it in GitHub Desktop.
Getting access to a view in AR
irb
irb(main):001:0> require 'active_record'
=> true
irb(main):002:0> ActiveRecord::Base.establish_connection(adapter: :postgresql, database: :view_demo)
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007ffc83b099d8>, @spec=#<ActiveRecord::ConnectionAdapters::ConnectionSpecification:0x007ffc84134d08 @config={:adapter=>:postgresql, :database=>:view_demo}, @adapter_method="postgresql_connection">, @checkout_timeout=5, @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x007ffc83b09c58 @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 ...>, @frequency=nil>, @size=5, @reserved_connections=#<ThreadSafe::Cache:0x007ffc83b0b440 @backend={}, @default_proc=nil>, @connections=[], @automatic_reconnect=true, @available=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Queue:0x007ffc83b03f10 @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 ...>, @cond=#<MonitorMixin::ConditionVariable:0x007ffc83b03ec0 @monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 ...>, @cond=#<Thread::ConditionVariable:0x007ffc83b03da8>>, @num_waiting=0, @queue=[]>>
irb(main):003:0> class BookOwnings < ActiveRecord::Base; self.primary_key = 'id'; end
=> nil
irb(main):004:0> BookOwnings.all
=> #<ActiveRecord::Relation [#<BookOwnings id: 1, first_name: "John", title: "The C Programming Language">, #<BookOwnings id: 2, first_name: "Kelly", title: "Parker's Wine Buyer's Guide">, #<BookOwnings id: 3, first_name: "Kelly", title: "The Emperor of Wine">]>
irb(main):005:0>
-- Create the view in SQL
-- (Note: The tables might well have been set up in AR; AR doesn't
-- provide a native way to create a view.)
drop database if exists view_demo;
create database view_demo;
\c view_demo;
create table users (id int, first_name text);
create table books (id int, user_id int, title text);
insert into users (id, first_name) values (1, 'John');
insert into users (id, first_name) values (2, 'Kelly');
insert into books (id, user_id, title) values (1, 1, 'The C Programming Language');
insert into books (id, user_id, title) values (2, 2, 'Parker''s Wine Buyer''s Guide');
insert into books (id, user_id, title) values (3, 2, 'The Emperor of Wine');
-- Create the view. Be careful to create something sensible for the primary key.
-- It could be a composite key or autoincrement. Here it's really a composite key;
-- the way the join works, there's a row for each book so might as well use the
-- book PK.
create view book_ownings as
select books.id as id, users.first_name as first_name, books.title as title from books join users on users.id = books.user_id;
select * from book_ownings;
-- produces
-- id | first_name | title
-- ----+------------+-----------------------------
-- 1 | John | The C Programming Language
-- 2 | Kelly | Parker's Wine Buyer's Guide
-- 3 | Kelly | The Emperor of Wine
-- (3 rows)
@jgn
Copy link
Author

jgn commented Feb 3, 2016

May be a bug in AR. Need to explicitly set PK w/ self.primary_key = 'id'; -- see rails/rails#16555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment