diff --git a/spec/avram/query_associations_spec.cr b/spec/avram/query_associations_spec.cr index 49424a49e..69de0cbc5 100644 --- a/spec/avram/query_associations_spec.cr +++ b/spec/avram/query_associations_spec.cr @@ -177,15 +177,18 @@ describe "Query associations" do result.should eq(product) end - it "handles aliases", focus: true do + it "handles aliases" do interviewer = UserFactory.create(&.available_for_hire(false).name("Interviewer")) interviewee = UserFactory.create(&.available_for_hire(true).name("Interviewee")) - UserFactory.create(&.available_for_hire(false).name("Employed")) + employed = UserFactory.create(&.available_for_hire(false).name("Employed")) InterviewFactory.create(&.interviewee(interviewee).interviewer(interviewer)) + InterviewFactory.create(&.interviewee(employed).interviewer(interviewer)) InterviewQuery.new - .where_interviewer(UserQuery.new.available_for_hire(false)) - .where_interviewee(UserQuery.new.available_for_hire(true)) - .to_prepared_sql.should eq("nothing") + .join(Avram::Join::Inner.new(:interviews, :users, alias_to: :interviewers, primary_key: :interviewer_id, foreign_key: :id)) + .join(Avram::Join::Inner.new(:interviews, :users, alias_to: :interviewees, primary_key: :interviewee_id, foreign_key: :id)) + .where_interviewer(UserQuery.new("interviewers").available_for_hire(false), auto_inner_join: false) + .where_interviewee(UserQuery.new("interviewees").available_for_hire(true), auto_inner_join: false) + .select_count.should eq(1) end end diff --git a/src/avram/base_query_template.cr b/src/avram/base_query_template.cr index 9c904fff9..f2bfdcc4f 100644 --- a/src/avram/base_query_template.cr +++ b/src/avram/base_query_template.cr @@ -4,6 +4,11 @@ class Avram::BaseQueryTemplate def_clone include Avram::Queryable({{ type }}) + getter table_name : String = {{ type }}.table_name + + def initialize(@table_name : String = {{ type }}.table_name) + end + {% if type.resolve.has_constant?("PRIMARY_KEY_NAME") %} include Avram::PrimaryKeyQueryable({{ type }}) {% end %} diff --git a/src/avram/queryable.cr b/src/avram/queryable.cr index ccbb9bdb0..c3beb882f 100644 --- a/src/avram/queryable.cr +++ b/src/avram/queryable.cr @@ -7,7 +7,7 @@ module Avram::Queryable(T) .select(schema_class.column_names) end - delegate :database, :table_name, :primary_key_name, to: T + delegate :database, :primary_key_name, to: T macro included def self.new_with_existing_query(query : Avram::QueryBuilder)