<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,7 @@
 *Edge*
 
+* Fix column collision with named_scope and :joins.  #46 [Duncan Beevers, Mark Catley]
+
 * db:migrate:down and :up update schema_migrations.  #369 [Michael Raidel, RaceCondition]
 
 * PostgreSQL: support :conditions =&gt; [':foo::integer', { :foo =&gt; 1 }] without treating the ::integer typecast as a bind variable.  [Tarmo T&#228;nav]</diff>
      <filename>activerecord/CHANGELOG</filename>
    </modified>
    <modified>
      <diff>@@ -1479,7 +1479,7 @@ module ActiveRecord #:nodoc:
 
         def construct_finder_sql(options)
           scope = scope(:find)
-          sql  = &quot;SELECT #{options[:select] || (scope &amp;&amp; scope[:select]) || (options[:joins] &amp;&amp; quoted_table_name + '.*') || '*'} &quot;
+          sql  = &quot;SELECT #{options[:select] || (scope &amp;&amp; scope[:select]) || ((options[:joins] || (scope &amp;&amp; scope[:joins])) &amp;&amp; quoted_table_name + '.*') || '*'} &quot;
           sql &lt;&lt; &quot;FROM #{(scope &amp;&amp; scope[:from]) || options[:from] || quoted_table_name} &quot;
 
           add_joins!(sql, options, scope)</diff>
      <filename>activerecord/lib/active_record/base.rb</filename>
    </modified>
    <modified>
      <diff>@@ -87,6 +87,16 @@ class MethodScopingTest &lt; ActiveRecord::TestCase
     assert_equal 1, scoped_developers.size
   end
 
+  def test_scoped_find_joins
+    scoped_developers = Developer.with_scope(:find =&gt; { :joins =&gt; 'JOIN developers_projects ON id = developer_id' } ) do
+      Developer.find(:all, :conditions =&gt; 'developers_projects.project_id = 2')
+    end
+    assert scoped_developers.include?(developers(:david))
+    assert !scoped_developers.include?(developers(:jamis))
+    assert_equal 1, scoped_developers.size
+    assert_equal developers(:david).attributes, scoped_developers.first.attributes
+  end
+
   def test_scoped_count_include
     # with the include, will retrieve only developers for the given project
     Developer.with_scope(:find =&gt; { :include =&gt; :projects }) do</diff>
      <filename>activerecord/test/cases/method_scoping_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,7 +6,7 @@ require 'models/reply'
 require 'models/author'
 
 class NamedScopeTest &lt; ActiveRecord::TestCase
-  fixtures :posts, :authors, :topics, :comments
+  fixtures :posts, :authors, :topics, :comments, :author_addresses
 
   def test_implements_enumerable
     assert !Topic.find(:all).empty?
@@ -83,6 +83,25 @@ class NamedScopeTest &lt; ActiveRecord::TestCase
     assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
   end
 
+  def test_scopes_with_joins
+    address = author_addresses(:david_address)
+    posts_with_authors_at_address = Post.find(
+      :all, :joins =&gt; 'JOIN authors ON authors.id = posts.author_id',
+      :conditions =&gt; [ 'authors.author_address_id = ?', address.id ]
+    )
+    assert_equal posts_with_authors_at_address, Post.with_authors_at_address(address)
+  end
+
+  def test_scopes_with_joins_respects_custom_select
+    address = author_addresses(:david_address)
+    posts_with_authors_at_address_titles = Post.find(:all,
+      :select =&gt; 'title',
+      :joins =&gt; 'JOIN authors ON authors.id = posts.author_id',
+      :conditions =&gt; [ 'authors.author_address_id = ?', address.id ]
+    )
+    assert_equal posts_with_authors_at_address_titles, Post.with_authors_at_address(address).find(:all, :select =&gt; 'title')
+  end
+
   def test_extensions
     assert_equal 1, Topic.anonymous_extension.one
     assert_equal 2, Topic.named_extension.two</diff>
      <filename>activerecord/test/cases/named_scope_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,11 @@
 class Post &lt; ActiveRecord::Base
   named_scope :containing_the_letter_a, :conditions =&gt; &quot;body LIKE '%a%'&quot;
-  
+  named_scope :with_authors_at_address, lambda { |address| {
+      :conditions =&gt; [ 'authors.author_address_id = ?', address.id ],
+      :joins =&gt; 'JOIN authors ON authors.id = posts.author_id'
+    }
+  }
+
   belongs_to :author do
     def greeting
       &quot;hello&quot;</diff>
      <filename>activerecord/test/models/post.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>bb6e8eea5a8190aaab67da0a7efedb3bb3d9fccb</id>
    </parent>
  </parents>
  <author>
    <name>Mark Catley</name>
    <email>mark@nexx.co.nz</email>
  </author>
  <url>http://github.com/rails/rails/commit/0fd3e4cd2b2b1b31304a922dc65284d5363f78b6</url>
  <id>0fd3e4cd2b2b1b31304a922dc65284d5363f78b6</id>
  <committed-date>2008-06-22T19:21:15-07:00</committed-date>
  <authored-date>2008-06-21T04:41:30-07:00</authored-date>
  <message>Fix column collision with named_scope and :joins.  [#46 state:resolved]</message>
  <tree>512a7018a967141c3762c0553e25f5dc4851e70f</tree>
  <committer>
    <name>Jeremy Kemper</name>
    <email>jeremy@bitsweat.net</email>
  </committer>
</commit>
