Skip to content

Instantly share code, notes, and snippets.

@avsej
Created April 22, 2014 16:00
Show Gist options
  • Save avsej/11184702 to your computer and use it in GitHub Desktop.
Save avsej/11184702 to your computer and use it in GitHub Desktop.
require 'active_record'
require 'sqlite3'
require 'squeel'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test.db')
ActiveRecord::Base.connection.tap do |c| # !> `&' interpreted as argument prefix
c.create_table(:posts, force: true) do |t| # !> method redefined; discarding old eval_scope
t.string :title
t.text :body
t.string :status
end
c.create_table(:events, force: true) do |t|
t.string :title
t.timestamp :when
t.string :status
end
# !> method redefined; discarding old where
c.create_table(:quotes, force: true) do |t|
t.string :author # !> `&' interpreted as argument prefix
t.string :body
end # !> shadowing outer local variable - reflection
c.create_table(:comments, force: true) do |t|
t.text :body
t.references :commentable, polymorphic: true
end
end
class Post < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Event < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Quote < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
def self.active_posts_and_events
joins{ commentable(Post).outer }.joins{ commentable(Event).outer }
.where{ (posts.status == 'active') | (events.status == 'active') }
.preload(:commentable)
end
end
Comment.create(body: 'comment-1', commentable: Event.new(title: 'event-1', status: 'inactive'))
Comment.create(body: 'comment-2', commentable: Event.new(title: 'event-2', status: 'active'))
Comment.create(body: 'comment-3', commentable: Post.new(title: 'post-1', status: 'inactive'))
Comment.create(body: 'comment-4', commentable: Post.new(title: 'post-2', status: 'active'))
Comment.create(body: 'comment-5', commentable: Quote.new(body: 'quote-1'))
Comment.create(body: 'comment-6', commentable: Quote.new(body: 'quote-2'))
Comment.create(body: 'comment-7', commentable: Event.new(title: 'event-3', status: 'active')) # !> assigned but unused variable - aliased_table
Comment.create(body: 'comment-8', commentable: Post.new(title: 'post-3', status: 'active'))
Comment.active_posts_and_events # !> `&' interpreted as argument prefix
# => [#<Comment id: 2, body: "comment-2", commentable_id: 2, commentable_type: "Event">,
# #<Comment id: 4, body: "comment-4", commentable_id: 2, commentable_type: "Post">,
# #<Comment id: 7, body: "comment-7", commentable_id: 3, commentable_type: "Event">,
# #<Comment id: 8, body: "comment-8", commentable_id: 3, commentable_type: "Post">]
puts Comment.active_posts_and_events.to_sql
# >> SELECT "comments".* FROM "comments" LEFT OUTER JOIN "posts" ON "posts"."id" = "comments"."commentable_id" AND "comments"."commentable_type" = 'Post' LEFT OUTER JOIN "events" ON "events"."id" = "comments"."commentable_id" AND "comments"."commentable_type" = 'Event' WHERE (("posts"."status" = 'active' OR "events"."status" = 'active'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment