[PATCH] do not ignore polymorphic type in join statement

6 views
Skip to first unread message

ara.t.howard

unread,
Jun 4, 2008, 1:24:27 PM6/4/08
to Composite Keys for ActiveRecord
i sent this to nic a while back but wanted to bump it here too as it
doesn't look like it got applied to the git repo



diff --git a/lib/composite_primary_keys/associations.rb b/lib/
composite_primary_keys/associations.rb
index a95d767..3bcbb30 100644
--- a/lib/composite_primary_keys/associations.rb
+++ b/lib/composite_primary_keys/associations.rb
@@ -376,31 +376,37 @@ module ActiveRecord::Associations
return conditions
end

- def construct_joins(custom_joins = nil)
+ def construct_joins(custom_joins = nil)
polymorphic_join = nil
if @reflection.through_reflection.options[:as] ||
@reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key =
@reflection.source_reflection.primary_key_name
+ if @reflection.options[:source_type]
+ polymorphic_join = "AND %s.%s = %s" % [
+ @reflection.through_reflection.table_name,
"#{@reflection.source_reflection.options[:foreign_type]}",
+
@owner.class.quote_value(@reflection.options[:source_type])
+ ]
+ end
else
reflection_primary_key =
@reflection.source_reflection.primary_key_name
source_primary_key = @reflection.klass.primary_key
if @reflection.source_reflection.options[:as]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.table_name,
"#{@reflection.source_reflection.options[:as]}_type",
-
@owner.class.quote_value(@reflection.through_reflection.klass.name)
- ]
- end
- end
-
- "INNER JOIN %s ON %s %s #{@reflection.options[:joins]}
#{custom_joins}" % [
- @reflection.through_reflection.table_name,
- composite_join_clause(
- full_keys(@reflection.table_name,
reflection_primary_key),
- full_keys(@reflection.through_reflection.table_name,
source_primary_key)),
- polymorphic_join
+
@owner.class.quote_value(@reflection.through_reflection.klass.name)
]
+ end
+ end
+
+ "INNER JOIN %s ON %s %s #{@reflection.options[:joins]}
#{custom_joins}" % [
+ @reflection.through_reflection.table_name,
+ composite_join_clause(
+ full_keys(@reflection.table_name, reflection_primary_key),
+ full_keys(@reflection.through_reflection.table_name,
source_primary_key)),
+ polymorphic_join
+ ]
end
-
+
def construct_sql
case
when @reflection.options[:finder_sql]



ara.t.howard

unread,
Jun 4, 2008, 1:33:36 PM6/4/08
to Composite Keys for ActiveRecord

because google seems to have fubar'd the formatting i'll post here
too:

http://s3.amazonaws.com/drawohara.com.patch/cpk-2008-06-04.patch


Nic Williams

unread,
Jun 4, 2008, 5:32:47 PM6/4/08
to compos...@googlegroups.com
Sorry Ara it must have got lost in my "had a baby" email backlog. But the mailing list is the place for patches, yes. Darrin will look after you.

Nic
--
Dr Nic Williams
* Learn TextMate for Rails2 - http://peepcode.com/products/textmate-for-rails-2 *
Training in Ruby/Rails/JavaScript - http://drnicacademy.com
Fun with Ruby/Rails/Javascript - http://drnicwilliams.com

darrin

unread,
Jun 4, 2008, 6:42:05 PM6/4/08
to Composite Keys for ActiveRecord
Hey Nic did my email get lost too:) Can you add me as a collaborator
on the github repo? I'm still getting permission denied.

On Jun 4, 4:32 pm, "Nic Williams" <drnicwilli...@gmail.com> wrote:
> Sorry Ara it must have got lost in my "had a baby" email backlog. But the
> mailing list is the place for patches, yes. Darrin will look after you.
> Nic
>

Nic Williams

unread,
Jun 4, 2008, 6:44:49 PM6/4/08
to compos...@googlegroups.com
Sorry, fixed.

ara.t.howard

unread,
Jun 4, 2008, 8:45:40 PM6/4/08
to Composite Keys for ActiveRecord


On Jun 4, 3:32 pm, "Nic Williams" <drnicwilli...@gmail.com> wrote:
> Sorry Ara it must have got lost in my "had a baby" email backlog. But the
> mailing list is the place for patches, yes. Darrin will look after you.
> Nic
>


oh? congratulations then!

i've been there and completely understand. don't worry - it gets
*much* easier ;-)

pics?

ara.t.howard

unread,
Jun 4, 2008, 8:47:21 PM6/4/08
to Composite Keys for ActiveRecord


On Jun 4, 4:42 pm, darrin <darrin.ho...@gmail.com> wrote:
> Hey Nic did my email get lost too:) Can you add me as a collaborator
> on the github repo? I'm still getting permission denied.

what's the gig with that? i pulled from you - but i assume you'll
merge back into the main repo?

git is going to confuse more that one project ;-(

Nic Williams

unread,
Jun 4, 2008, 9:02:16 PM6/4/08
to compos...@googlegroups.com
My repo is the main one, darrin just has commit rights, like a centralised svn. He is also admin on the rubyforge project so he can push gems etc. If you want commit rights too (and promise to write tests for new stuff) I can add you if Darrin's keen.

Ultimately Darrin is the maintainer of the project. I think I just keep getting in the way unnecessarily + accidently :)

Nic
--
Dr Nic Williams

ara.t.howard

unread,
Jun 6, 2008, 12:17:38 AM6/6/08
to Composite Keys for ActiveRecord

On Jun 4, 7:02 pm, "Nic Williams" <drnicwilli...@gmail.com> wrote:
> My repo is the main one, darrin just has commit rights, like a centralised
> svn. He is also admin on the rubyforge project so he can push gems etc. If
> you want commit rights too (and promise to write tests for new stuff) I can
> add you if Darrin's keen.
> Ultimately Darrin is the maintainer of the project. I think I just keep
> getting in the way unnecessarily + accidently :)
>
> Nic
>

sure - i'm doing a migration of a big rails app now that uses
composite primary keys so i may have more to contribute. i'm on
github but haven't used it much yet. what do you need from me?

cheers.

Nic Williams

unread,
Jun 6, 2008, 12:19:18 AM6/6/08
to compos...@googlegroups.com
What's your github user name?

Also, have a look at the test rake tasks - there are ways to run tests against a local edge rails and against diff gem versions.

ara.t.howard

unread,
Jun 6, 2008, 1:42:26 PM6/6/08
to Composite Keys for ActiveRecord


On Jun 5, 10:19 pm, "Nic Williams" <drnicwilli...@gmail.com> wrote:
> What's your github user name?


ahoward
Reply all
Reply to author
Forward
0 new messages