Last active
August 29, 2015 14:00
-
-
Save avsej/11189599 to your computer and use it in GitHub Desktop.
select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
where do | |
[Post, Event].map do |model| | |
(commentable_type == model.name) & (commentable_id.in model.where(status: 'active')) | |
end.reduce(:|) | |
end.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')) # !> assigned but unused variable - aliased_table | |
Comment.create(body: 'comment-7', commentable: Event.new(title: 'event-3', status: 'active')) | |
Comment.create(body: 'comment-8', commentable: Post.new(title: 'post-3', status: 'active')) # !> `&' interpreted as argument prefix | |
Comment.active_posts_and_events | |
# => [#<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" WHERE | |
# ((("comments"."commentable_type" = 'Post' AND "comments"."commentable_id" IN (SELECT "posts"."id" FROM "posts" WHERE "posts"."status" = 'active')) | |
# OR ("comments"."commentable_type" = 'Event' AND "comments"."commentable_id" IN (SELECT "events"."id" FROM "events" WHERE "events"."status" = 'active')))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment