Skip to content
This repository
Browse code

Convert association macros to the new syntax

  • Loading branch information...
commit e1cfa6e0705f37b5f8cb885380f3a47405c24a9d 1 parent 7a271a8
Jon Leighton authored July 13, 2012
2  activerecord/lib/active_record/associations/preloader/association.rb
@@ -93,7 +93,7 @@ def associated_records_by_owner
93 93
         end
94 94
 
95 95
         def reflection_scope
96  
-          @reflection_scope ||= reflection.scope ? klass.unscoped.instance_exec(&reflection.scope) : klass.unscoped
  96
+          @reflection_scope ||= reflection.scope ? klass.unscoped.instance_exec(nil, &reflection.scope) : klass.unscoped
97 97
         end
98 98
 
99 99
         def build_scope
72  activerecord/test/models/author.rb
... ...
@@ -1,12 +1,12 @@
1 1
 class Author < ActiveRecord::Base
2 2
   has_many :posts
3 3
   has_many :very_special_comments, :through => :posts
4  
-  has_many :posts_with_comments, :include => :comments, :class_name => "Post"
5  
-  has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type"
6  
-  has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
7  
-  has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1
8  
-  has_many :posts_with_categories, :include => :categories, :class_name => "Post"
9  
-  has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
  4
+  has_many :posts_with_comments, -> { includes(:comments) }, :class_name => "Post"
  5
+  has_many :popular_grouped_posts, -> { includes(:comments).group("type").having("SUM(comments_count) > 1").select("type") }, :class_name => "Post"
  6
+  has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order('comments.id') }, :class_name => "Post"
  7
+  has_many :posts_sorted_by_id_limited, -> { order('posts.id').limit(1) }, :class_name => "Post"
  8
+  has_many :posts_with_categories, -> { includes(:categories) }, :class_name => "Post"
  9
+  has_many :posts_with_comments_and_categories, -> { includes(:comments, :categories).order("posts.id") }, :class_name => "Post"
10 10
   has_many :posts_containing_the_letter_a, :class_name => "Post"
11 11
   has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension
12 12
     def testing_proxy_owner
@@ -19,30 +19,30 @@ def testing_proxy_target
19 19
       proxy_target
20 20
     end
21 21
   end
22  
-  has_one  :post_about_thinking, :class_name => 'Post', :conditions => "posts.title like '%thinking%'"
23  
-  has_one  :post_about_thinking_with_last_comment, :class_name => 'Post', :conditions => "posts.title like '%thinking%'", :include => :last_comment
  22
+  has_one  :post_about_thinking, -> { where("posts.title like '%thinking%'") }, :class_name => 'Post'
  23
+  has_one  :post_about_thinking_with_last_comment, -> { where("posts.title like '%thinking%'").includes(:last_comment) }, :class_name => 'Post'
24 24
   has_many :comments, :through => :posts
25 25
   has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
26  
-  has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
27  
-  has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post
  26
+  has_many :comments_with_order_and_conditions, -> { order('comments.body').where("comments.body like 'Thank%'") }, :through => :posts, :source => :comments
  27
+  has_many :comments_with_include, -> { includes(:post) }, :through => :posts, :source => :comments
28 28
 
29 29
   has_many :posts_with_scope_block, -> { order('posts.id').limit(1) }, :class_name => "Post"
30 30
 
31 31
   has_many :first_posts
32  
-  has_many :comments_on_first_posts, :through => :first_posts, :source => :comments, :order => 'posts.id desc, comments.id asc'
  32
+  has_many :comments_on_first_posts, -> { order('posts.id desc, comments.id asc') }, :through => :first_posts, :source => :comments
33 33
 
34 34
   has_one :first_post
35  
-  has_one :comment_on_first_post,  :through => :first_post, :source => :comments, :order => 'posts.id desc, comments.id asc'
  35
+  has_one :comment_on_first_post, -> { order('posts.id desc, comments.id asc') }, :through => :first_post, :source => :comments
36 36
 
37  
-  has_many :thinking_posts, :class_name => 'Post', :conditions => { :title => 'So I was thinking' }, :dependent => :delete_all
38  
-  has_many :welcome_posts,  :class_name => 'Post', :conditions => { :title => 'Welcome to the weblog' }
  37
+  has_many :thinking_posts, -> { where(:title => 'So I was thinking') }, :dependent => :delete_all, :class_name => 'Post'
  38
+  has_many :welcome_posts, -> { where(:title => 'Welcome to the weblog') }, :class_name => 'Post'
39 39
 
40  
-  has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
41  
-  has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
  40
+  has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments
  41
+  has_many :limited_comments, -> { limit(1) }, :through => :posts, :source => :comments
42 42
   has_many :funky_comments, :through => :posts, :source => :comments
43  
-  has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
44  
-  has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
45  
-  has_many :readonly_comments, :through => :posts, :source => :comments, :readonly => true
  43
+  has_many :ordered_uniq_comments, -> { uniq.order('comments.id') }, :through => :posts, :source => :comments
  44
+  has_many :ordered_uniq_comments_desc, -> { uniq.order('comments.id DESC') }, :through => :posts, :source => :comments
  45
+  has_many :readonly_comments, -> { readonly }, :through => :posts, :source => :comments
46 46
 
47 47
   has_many :special_posts
48 48
   has_many :special_post_comments, :through => :special_posts, :source => :comments
@@ -50,16 +50,15 @@ def testing_proxy_target
50 50
   has_many :sti_posts, :class_name => 'StiPost'
51 51
   has_many :sti_post_comments, :through => :sti_posts, :source => :comments
52 52
 
53  
-  has_many :special_nonexistant_posts, :class_name => "SpecialPost", :conditions => "posts.body = 'nonexistant'"
54  
-  has_many :special_nonexistant_post_comments, :through => :special_nonexistant_posts, :source => :comments, :conditions => { 'comments.post_id' => 0 }
  53
+  has_many :special_nonexistant_posts, -> { where("posts.body = 'nonexistant'") }, :class_name => "SpecialPost"
  54
+  has_many :special_nonexistant_post_comments, -> { where('comments.post_id' => 0) }, :through => :special_nonexistant_posts, :source => :comments
55 55
   has_many :nonexistant_comments, :through => :posts
56 56
 
57  
-  has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'"
  57
+  has_many :hello_posts, -> { where "posts.body = 'hello'" }, :class_name => "Post"
58 58
   has_many :hello_post_comments, :through => :hello_posts, :source => :comments
59  
-  has_many :posts_with_no_comments, :class_name => 'Post', :conditions => { 'comments.id' => nil }, :include => :comments
  59
+  has_many :posts_with_no_comments, -> { where('comments.id' => nil).includes(:comments) }, :class_name => 'Post'
60 60
 
61  
-  has_many :hello_posts_with_hash_conditions, :class_name => "Post",
62  
-:conditions => {:body => 'hello'}
  61
+  has_many :hello_posts_with_hash_conditions, -> { where(:body => 'hello') }, :class_name => "Post"
63 62
   has_many :hello_post_comments_with_hash_conditions, :through =>
64 63
 :hello_posts_with_hash_conditions, :source => :comments
65 64
 
@@ -86,29 +85,31 @@ def testing_proxy_target
86 85
   has_many :special_categories, :through => :special_categorizations, :source => :category
87 86
   has_one  :special_category,   :through => :special_categorizations, :source => :category
88 87
 
89  
-  has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' }
  88
+  has_many :categories_like_general, -> { where(:name => 'General') }, :through => :categorizations, :source => :category, :class_name => 'Category'
90 89
 
91 90
   has_many :categorized_posts, :through => :categorizations, :source => :post
92  
-  has_many :unique_categorized_posts, :through => :categorizations, :source => :post, :uniq => true
  91
+  has_many :unique_categorized_posts, -> { uniq }, :through => :categorizations, :source => :post
93 92
 
94 93
   has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
95 94
 
96 95
   has_many :author_favorites
97  
-  has_many :favorite_authors, :through => :author_favorites, :order => 'name'
  96
+  has_many :favorite_authors, -> { order('name') }, :through => :author_favorites
98 97
 
99 98
   has_many :tagging,         :through => :posts
100 99
   has_many :taggings,        :through => :posts
101 100
   has_many :tags,            :through => :posts
102  
-  has_many :similar_posts,   :through => :tags,  :source => :tagged_posts, :uniq => true
103  
-  has_many :distinct_tags,   :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name"
104 101
   has_many :post_categories, :through => :posts, :source => :categories
105 102
   has_many :tagging_tags,    :through => :taggings, :source => :tag
  103
+
  104
+  has_many :similar_posts, -> { uniq }, :through => :tags, :source => :tagged_posts
  105
+  has_many :distinct_tags, -> { select("DISTINCT tags.*").order("tags.name") }, :through => :posts, :source => :tags
  106
+
106 107
   has_many :tags_with_primary_key, :through => :posts
107 108
 
108 109
   has_many :books
109 110
   has_many :subscriptions,        :through => :books
110  
-  has_many :subscribers,          :through => :subscriptions, :order => "subscribers.nick" # through has_many :through (on through reflection)
111  
-  has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick"
  111
+  has_many :subscribers, -> { order("subscribers.nick") }, :through => :subscriptions
  112
+  has_many :distinct_subscribers, -> { select("DISTINCT subscribers.*").order("subscribers.nick") }, :through => :subscriptions, :source => :subscriber
112 113
 
113 114
   has_one :essay, :primary_key => :name, :as => :writer
114 115
   has_one :essay_category, :through => :essay, :source => :category
@@ -132,12 +133,11 @@ def testing_proxy_target
132 133
 
133 134
   has_many :category_post_comments, :through => :categories, :source => :post_comments
134 135
 
135  
-  has_many :misc_posts, :class_name => 'Post',
136  
-           :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } }
  136
+  has_many :misc_posts, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) }, :class_name => 'Post'
137 137
   has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags
138 138
 
139  
-  has_many :misc_post_first_blue_tags_2, :through => :posts, :source => :first_blue_tags_2,
140  
-           :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } }
  139
+  has_many :misc_post_first_blue_tags_2, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) },
  140
+           :through => :posts, :source => :first_blue_tags_2
141 141
 
142 142
   has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude'
143 143
   has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments
2  activerecord/test/models/book.rb
@@ -2,7 +2,7 @@ class Book < ActiveRecord::Base
2 2
   has_many :authors
3 3
 
4 4
   has_many :citations, :foreign_key => 'book1_id'
5  
-  has_many :references, :through => :citations, :source => :reference_of, :uniq => true
  5
+  has_many :references, -> { uniq }, :through => :citations, :source => :reference_of
6 6
 
7 7
   has_many :subscriptions
8 8
   has_many :subscribers, :through => :subscriptions
6  activerecord/test/models/car.rb
... ...
@@ -1,11 +1,11 @@
1 1
 class Car < ActiveRecord::Base
2 2
 
3 3
   has_many :bulbs
4  
-  has_many :foo_bulbs, :class_name => "Bulb", :conditions => { :name => 'foo' }
5  
-  has_many :frickinawesome_bulbs, :class_name => "Bulb", :conditions => { :frickinawesome => true }
  4
+  has_many :foo_bulbs, -> { where(:name => 'foo') }, :class_name => "Bulb"
  5
+  has_many :frickinawesome_bulbs, -> { where :frickinawesome => true }, :class_name => "Bulb"
6 6
 
7 7
   has_one :bulb
8  
-  has_one :frickinawesome_bulb, :class_name => "Bulb", :conditions => { :frickinawesome => true }
  8
+  has_one :frickinawesome_bulb, -> { where :frickinawesome => true }, :class_name => "Bulb"
9 9
 
10 10
   has_many :tyres
11 11
   has_many :engines, :dependent => :destroy
18  activerecord/test/models/category.rb
@@ -2,20 +2,20 @@ class Category < ActiveRecord::Base
2 2
   has_and_belongs_to_many :posts
3 3
   has_and_belongs_to_many :special_posts, :class_name => "Post"
4 4
   has_and_belongs_to_many :other_posts, :class_name => "Post"
5  
-  has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, :class_name => "Post", :include => :authors, :order => "authors.id"
  5
+  has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, -> { includes(:authors).order("authors.id") }, :class_name => "Post"
6 6
 
7  
-  has_and_belongs_to_many(:select_testing_posts,
  7
+  has_and_belongs_to_many :select_testing_posts,
  8
+                          -> { select 'posts.*, 1 as correctness_marker' },
8 9
                           :class_name => 'Post',
9 10
                           :foreign_key => 'category_id',
10  
-                          :association_foreign_key => 'post_id',
11  
-                          :select => 'posts.*, 1 as correctness_marker')
  11
+                          :association_foreign_key => 'post_id'
12 12
 
13 13
   has_and_belongs_to_many :post_with_conditions,
14  
-                          :class_name => 'Post',
15  
-                          :conditions => { :title => 'Yet Another Testing Title' }
  14
+                          -> { where :title => 'Yet Another Testing Title' },
  15
+                          :class_name => 'Post'
16 16
 
17  
-  has_and_belongs_to_many :popular_grouped_posts, :class_name => "Post", :group => "posts.type", :having => "sum(comments.post_id) > 2", :include => :comments
18  
-  has_and_belongs_to_many :posts_grouped_by_title, :class_name => "Post", :group => "title", :select => "title"
  17
+  has_and_belongs_to_many :popular_grouped_posts, -> { group("posts.type").having("sum(comments.post_id) > 2").includes(:comments) }, :class_name => "Post"
  18
+  has_and_belongs_to_many :posts_grouped_by_title, -> { group("title").select("title") }, :class_name => "Post"
19 19
 
20 20
   def self.what_are_you
21 21
     'a category...'
@@ -25,7 +25,7 @@ def self.what_are_you
25 25
   has_many :post_comments, :through => :posts, :source => :comments
26 26
 
27 27
   has_many :authors, :through => :categorizations
28  
-  has_many :authors_with_select, :through => :categorizations, :source => :author, :select => 'authors.*, categorizations.post_id'
  28
+  has_many :authors_with_select, -> { select 'authors.*, categorizations.post_id' }, :through => :categorizations, :source => :author
29 29
 
30 30
   scope :general, -> { where(:name => 'General') }
31 31
 end
54  activerecord/test/models/company.rb
@@ -36,23 +36,23 @@ class Client < ::Company
36 36
 end
37 37
 
38 38
 class Firm < Company
39  
-  has_many :clients, :order => "id", :dependent => :destroy, :counter_sql =>
  39
+  has_many :clients, -> { order "id" }, :dependent => :destroy, :counter_sql =>
40 40
       "SELECT COUNT(*) FROM companies WHERE firm_id = 1 " +
41 41
       "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )",
42 42
       :before_remove => :log_before_remove,
43 43
       :after_remove  => :log_after_remove
44 44
   has_many :unsorted_clients, :class_name => "Client"
45 45
   has_many :unsorted_clients_with_symbol, :class_name => :Client
46  
-  has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
47  
-  has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
48  
-  has_many :clients_ordered_by_name, :order => "name", :class_name => "Client"
  46
+  has_many :clients_sorted_desc, -> { order "id DESC" }, :class_name => "Client"
  47
+  has_many :clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client"
  48
+  has_many :clients_ordered_by_name, -> { order "name" }, :class_name => "Client"
49 49
   has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
50  
-  has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy
51  
-  has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all
52  
-  has_many :limited_clients, :class_name => "Client", :limit => 1
53  
-  has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => proc { "rating > #{rating}" }
54  
-  has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
55  
-  has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id"
  50
+  has_many :dependent_clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :dependent => :destroy
  51
+  has_many :exclusively_dependent_clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
  52
+  has_many :limited_clients, -> { limit 1 }, :class_name => "Client"
  53
+  has_many :clients_with_interpolated_conditions, ->(firm) { where "rating > #{firm.rating}" }, :class_name => "Client"
  54
+  has_many :clients_like_ms, -> { where("name = 'Microsoft'").order("id") }, :class_name => "Client"
  55
+  has_many :clients_like_ms_with_hash_conditions, -> { where(:name => 'Microsoft').order("id") }, :class_name => "Client"
56 56
   has_many :clients_using_sql, :class_name => "Client", :finder_sql => proc { "SELECT * FROM companies WHERE client_of = #{id}" }
57 57
   has_many :clients_using_counter_sql, :class_name => "Client",
58 58
            :finder_sql  => proc { "SELECT * FROM companies WHERE client_of = #{id} " },
@@ -65,31 +65,31 @@ class Firm < Company
65 65
            :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
66 66
   has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
67 67
   has_many :plain_clients, :class_name => 'Client'
68  
-  has_many :readonly_clients, :class_name => 'Client', :readonly => true
  68
+  has_many :readonly_clients, -> { readonly }, :class_name => 'Client'
69 69
   has_many :clients_using_primary_key, :class_name => 'Client',
70 70
            :primary_key => 'name', :foreign_key => 'firm_name'
71 71
   has_many :clients_using_primary_key_with_delete_all, :class_name => 'Client',
72 72
            :primary_key => 'name', :foreign_key => 'firm_name', :dependent => :delete_all
73  
-  has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id"
74  
-  has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name"
  73
+  has_many :clients_grouped_by_firm_id, -> { group("firm_id").select("firm_id") }, :class_name => "Client"
  74
+  has_many :clients_grouped_by_name, -> { group("name").select("name") }, :class_name => "Client"
75 75
 
76 76
   has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
77 77
   has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
78  
-  has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
79  
-  has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
  78
+  has_one :account_with_select, -> { select("id, firm_id") }, :foreign_key => "firm_id", :class_name=>'Account'
  79
+  has_one :readonly_account, -> { readonly }, :foreign_key => "firm_id", :class_name => "Account"
80 80
   # added order by id as in fixtures there are two accounts for Rails Core
81 81
   # Oracle tests were failing because of that as the second fixture was selected
82  
-  has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account", :order => "id"
  82
+  has_one :account_using_primary_key, -> { order('id') }, :primary_key => "firm_id", :class_name => "Account"
83 83
   has_one :account_using_foreign_and_primary_keys, :foreign_key => "firm_name", :primary_key => "name", :class_name => "Account"
84 84
   has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
85 85
 
86  
-  has_one :account_limit_500_with_hash_conditions, :foreign_key => "firm_id", :class_name => "Account", :conditions => { :credit_limit => 500 }
  86
+  has_one :account_limit_500_with_hash_conditions, -> { where :credit_limit => 500 }, :foreign_key => "firm_id", :class_name => "Account"
87 87
 
88 88
   has_one :unautosaved_account, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
89 89
   has_many :accounts
90 90
   has_many :unautosaved_accounts, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
91 91
 
92  
-  has_many :association_with_references, :class_name => 'Client', :references => :foo
  92
+  has_many :association_with_references, -> { references(:foo) }, :class_name => 'Client'
93 93
 
94 94
   def log
95 95
     @log ||= []
@@ -111,20 +111,20 @@ class DependentFirm < Company
111 111
 end
112 112
 
113 113
 class RestrictedFirm < Company
114  
-  has_one :account, :foreign_key => "firm_id", :dependent => :restrict, :order => "id"
115  
-  has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :restrict
  114
+  has_one :account, -> { order("id") }, :foreign_key => "firm_id", :dependent => :restrict
  115
+  has_many :companies, -> { order("id") }, :foreign_key => 'client_of', :dependent => :restrict
116 116
 end
117 117
 
118 118
 class Client < Company
119 119
   belongs_to :firm, :foreign_key => "client_of"
120 120
   belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
121  
-  belongs_to :firm_with_select, :class_name => "Firm", :foreign_key => "firm_id", :select => "id"
  121
+  belongs_to :firm_with_select, -> { select("id") }, :class_name => "Firm", :foreign_key => "firm_id"
122 122
   belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
123  
-  belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
  123
+  belongs_to :firm_with_condition, -> { where "1 = ?", 1 }, :class_name => "Firm", :foreign_key => "client_of"
124 124
   belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name"
125 125
   belongs_to :firm_with_primary_key_symbols, :class_name => "Firm", :primary_key => :name, :foreign_key => :firm_name
126  
-  belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true
127  
-  belongs_to :bob_firm, :class_name => "Firm", :foreign_key => "client_of", :conditions => { :name => "Bob" }
  126
+  belongs_to :readonly_firm, -> { readonly }, :class_name => "Firm", :foreign_key => "firm_id"
  127
+  belongs_to :bob_firm, -> { where :name => "Bob" }, :class_name => "Firm", :foreign_key => "client_of"
128 128
   has_many :accounts, :through => :firm
129 129
   belongs_to :account
130 130
 
@@ -179,9 +179,9 @@ def private_method
179 179
 
180 180
 class ExclusivelyDependentFirm < Company
181 181
   has_one :account, :foreign_key => "firm_id", :dependent => :delete
182  
-  has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
183  
-  has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
184  
-  has_many :dependent_hash_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => {:name => 'BigShot Inc.'}
  182
+  has_many :dependent_sanitized_conditional_clients_of_firm, -> { order("id").where("name = 'BigShot Inc.'") }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
  183
+  has_many :dependent_conditional_clients_of_firm, -> { order("id").where("name = ?", 'BigShot Inc.') }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
  184
+  has_many :dependent_hash_conditional_clients_of_firm, -> { order("id").where(:name => 'BigShot Inc.') }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
185 185
 end
186 186
 
187 187
 class SpecialClient < Client
8  activerecord/test/models/company_in_module.rb
@@ -7,10 +7,10 @@ class Company < ActiveRecord::Base
7 7
     end
8 8
 
9 9
     class Firm < Company
10  
-      has_many :clients, :order => "id", :dependent => :destroy
11  
-      has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
12  
-      has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
13  
-      has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
  10
+      has_many :clients, -> { order("id") }, :dependent => :destroy
  11
+      has_many :clients_sorted_desc, -> { order("id DESC") }, :class_name => "Client"
  12
+      has_many :clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client"
  13
+      has_many :clients_like_ms, -> { where("name = 'Microsoft'").order("id") }, :class_name => "Client"
14 14
       has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
15 15
 
16 16
       has_one :account, :class_name => 'MyApplication::Billing::Account', :dependent => :destroy
22  activerecord/test/models/developer.rb
@@ -20,22 +20,22 @@ def find_most_recent
20 20
   end
21 21
 
22 22
   has_and_belongs_to_many :projects_extended_by_name,
  23
+      -> { extending(DeveloperProjectsAssociationExtension) },
23 24
       :class_name => "Project",
24 25
       :join_table => "developers_projects",
25  
-      :association_foreign_key => "project_id",
26  
-      :extend => DeveloperProjectsAssociationExtension
  26
+      :association_foreign_key => "project_id"
27 27
 
28 28
   has_and_belongs_to_many :projects_extended_by_name_twice,
  29
+      -> { extending(DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2) },
29 30
       :class_name => "Project",
30 31
       :join_table => "developers_projects",
31  
-      :association_foreign_key => "project_id",
32  
-      :extend => [DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2]
  32
+      :association_foreign_key => "project_id"
33 33
 
34 34
   has_and_belongs_to_many :projects_extended_by_name_and_block,
  35
+      -> { extending(DeveloperProjectsAssociationExtension) },
35 36
       :class_name => "Project",
36 37
       :join_table => "developers_projects",
37  
-      :association_foreign_key => "project_id",
38  
-      :extend => DeveloperProjectsAssociationExtension do
  38
+      :association_foreign_key => "project_id" do
39 39
         def find_least_recent
40 40
           scoped(:order => "id ASC").first
41 41
         end
@@ -175,14 +175,14 @@ class ModuleIncludedPoorDeveloperCalledJamis < DeveloperCalledJamis
175 175
 
176 176
 class EagerDeveloperWithDefaultScope < ActiveRecord::Base
177 177
   self.table_name = 'developers'
178  
-  has_and_belongs_to_many :projects, :foreign_key => 'developer_id', :join_table => 'developers_projects', :order => 'projects.id'
  178
+  has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
179 179
 
180 180
   default_scope { includes(:projects) }
181 181
 end
182 182
 
183 183
 class EagerDeveloperWithClassMethodDefaultScope < ActiveRecord::Base
184 184
   self.table_name = 'developers'
185  
-  has_and_belongs_to_many :projects, :foreign_key => 'developer_id', :join_table => 'developers_projects', :order => 'projects.id'
  185
+  has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
186 186
 
187 187
   def self.default_scope
188 188
     includes(:projects)
@@ -191,21 +191,21 @@ def self.default_scope
191 191
 
192 192
 class EagerDeveloperWithLambdaDefaultScope < ActiveRecord::Base
193 193
   self.table_name = 'developers'
194  
-  has_and_belongs_to_many :projects, :foreign_key => 'developer_id', :join_table => 'developers_projects', :order => 'projects.id'
  194
+  has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
195 195
 
196 196
   default_scope lambda { includes(:projects) }
197 197
 end
198 198
 
199 199
 class EagerDeveloperWithBlockDefaultScope < ActiveRecord::Base
200 200
   self.table_name = 'developers'
201  
-  has_and_belongs_to_many :projects, :foreign_key => 'developer_id', :join_table => 'developers_projects', :order => 'projects.id'
  201
+  has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
202 202
 
203 203
   default_scope { includes(:projects) }
204 204
 end
205 205
 
206 206
 class EagerDeveloperWithCallableDefaultScope < ActiveRecord::Base
207 207
   self.table_name = 'developers'
208  
-  has_and_belongs_to_many :projects, :foreign_key => 'developer_id', :join_table => 'developers_projects', :order => 'projects.id'
  208
+  has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
209 209
 
210 210
   default_scope OpenStruct.new(:call => includes(:projects))
211 211
 end
2  activerecord/test/models/liquid.rb
... ...
@@ -1,5 +1,5 @@
1 1
 class Liquid < ActiveRecord::Base
2 2
   self.table_name = :liquid
3  
-  has_many :molecules, :uniq => true
  3
+  has_many :molecules, -> { uniq }
4 4
 end
5 5
 
6  activerecord/test/models/member.rb
@@ -5,8 +5,8 @@ class Member < ActiveRecord::Base
5 5
   has_many :fellow_members, :through => :club, :source => :members
6 6
   has_one :club, :through => :current_membership
7 7
   has_one :selected_club, :through => :selected_membership, :source => :club
8  
-  has_one :favourite_club, :through => :membership, :conditions => ["memberships.favourite = ?", true], :source => :club
9  
-  has_one :hairy_club, :through => :membership, :conditions => {:clubs => {:name => "Moustache and Eyebrow Fancier Club"}}, :source => :club
  8
+  has_one :favourite_club, -> { where "memberships.favourite = ?", true }, :through => :membership, :source => :club
  9
+  has_one :hairy_club, -> { where :clubs => {:name => "Moustache and Eyebrow Fancier Club"} }, :through => :membership, :source => :club
10 10
   has_one :sponsor, :as => :sponsorable
11 11
   has_one :sponsor_club, :through => :sponsor
12 12
   has_one :member_detail
@@ -27,7 +27,7 @@ class Member < ActiveRecord::Base
27 27
   has_many :current_memberships
28 28
   has_one :club_through_many, :through => :current_memberships, :source => :club
29 29
 
30  
-  has_many :current_memberships, :conditions => { :favourite => true }
  30
+  has_many :current_memberships, -> { where :favourite => true }
31 31
   has_many :clubs, :through => :current_memberships
32 32
 end
33 33
 
12  activerecord/test/models/person.rb
@@ -5,14 +5,14 @@ class Person < ActiveRecord::Base
5 5
 
6 6
   has_many :posts, :through => :readers
7 7
   has_many :secure_posts, :through => :secure_readers
8  
-  has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments,
9  
-                                    :conditions => 'comments.id is null', :references => :comments
  8
+  has_many :posts_with_no_comments, -> { includes(:comments).where('comments.id is null').references(:comments) },
  9
+                                    :through => :readers, :source => :post
10 10
 
11 11
   has_many :references
12 12
   has_many :bad_references
13  
-  has_many :fixed_bad_references, :conditions => { :favourite => true }, :class_name => 'BadReference'
14  
-  has_one  :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]
15  
-  has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id'
  13
+  has_many :fixed_bad_references, -> { where :favourite => true }, :class_name => 'BadReference'
  14
+  has_one  :favourite_reference, -> { where 'favourite=?', true }, :class_name => 'Reference'
  15
+  has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order('comments.id') }, :through => :readers, :source => :post
16 16
 
17 17
   has_many :jobs, :through => :references
18 18
   has_many :jobs_with_dependent_destroy,    :source => :job, :through => :references, :dependent => :destroy
@@ -109,4 +109,4 @@ def comments=(new_comments)
109 109
   def best_friend_first_name=(new_name)
110 110
     assign_attributes({ :best_friend_attributes => { :first_name => new_name } })
111 111
   end
112  
-end
  112
+end
6  activerecord/test/models/pirate.rb
... ...
@@ -1,7 +1,7 @@
1 1
 class Pirate < ActiveRecord::Base
2 2
   belongs_to :parrot, :validate => true
3 3
   belongs_to :non_validated_parrot, :class_name => 'Parrot'
4  
-  has_and_belongs_to_many :parrots, :validate => true, :order => 'parrots.id ASC'
  4
+  has_and_belongs_to_many :parrots, -> { order('parrots.id ASC') }, :validate => true
5 5
   has_and_belongs_to_many :non_validated_parrots, :class_name => 'Parrot'
6 6
   has_and_belongs_to_many :parrots_with_method_callbacks, :class_name => "Parrot",
7 7
     :before_add    => :log_before_add,
@@ -21,7 +21,7 @@ class Pirate < ActiveRecord::Base
21 21
   has_one :ship
22 22
   has_one :update_only_ship, :class_name => 'Ship'
23 23
   has_one :non_validated_ship, :class_name => 'Ship'
24  
-  has_many :birds, :order => 'birds.id ASC'
  24
+  has_many :birds, -> { order('birds.id ASC') }
25 25
   has_many :birds_with_method_callbacks, :class_name => "Bird",
26 26
     :before_add    => :log_before_add,
27 27
     :after_add     => :log_after_add,
@@ -34,7 +34,7 @@ class Pirate < ActiveRecord::Base
34 34
     :after_remove   => proc {|p,b| p.ship_log << "after_removing_proc_bird_#{b.id}"}
35 35
   has_many :birds_with_reject_all_blank, :class_name => "Bird"
36 36
 
37  
-  has_one :foo_bulb, :foreign_key => :car_id, :class_name => "Bulb", :conditions => { :name => 'foo' }
  37
+  has_one :foo_bulb, -> { where :name => 'foo' }, :foreign_key => :car_id, :class_name => "Bulb"
38 38
 
39 39
   accepts_nested_attributes_for :parrots, :birds, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
40 40
   accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
33  activerecord/test/models/post.rb
@@ -16,14 +16,14 @@ def greeting
16 16
     end
17 17
   end
18 18
 
19  
-  belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts
20  
-  belongs_to :author_with_address, :class_name => "Author", :foreign_key => :author_id, :include => :author_address
  19
+  belongs_to :author_with_posts, -> { includes(:posts) }, :class_name => "Author", :foreign_key => :author_id
  20
+  belongs_to :author_with_address, -> { includes(:author_address) }, :class_name => "Author", :foreign_key => :author_id
21 21
 
22 22
   def first_comment
23 23
     super.body
24 24
   end
25  
-  has_one :first_comment, :class_name => 'Comment', :order => 'id ASC'
26  
-  has_one :last_comment, :class_name => 'Comment', :order => 'id desc'
  25
+  has_one :first_comment, -> { order('id ASC') }, :class_name => 'Comment'
  26
+  has_one :last_comment, -> { order('id desc') }, :class_name => 'Comment'
27 27
 
28 28
   scope :with_special_comments, -> { joins(:comments).where(:comments => {:type => 'SpecialComment'}) }
29 29
   scope :with_very_special_comments, -> { joins(:comments).where(:comments => {:type => 'VerySpecialComment'}) }
@@ -47,13 +47,14 @@ def the_association
47 47
   has_many :author_categorizations, :through => :author, :source => :categorizations
48 48
   has_many :author_addresses, :through => :author
49 49
 
50  
-  has_many :comments_with_interpolated_conditions, :class_name => 'Comment',
51  
-    :conditions => proc { ["#{"#{aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome'] }
  50
+  has_many :comments_with_interpolated_conditions,
  51
+    ->(p) { where "#{"#{p.aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome' },
  52
+    :class_name => 'Comment'
52 53
 
53 54
   has_one  :very_special_comment
54  
-  has_one  :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post
  55
+  has_one  :very_special_comment_with_post, -> { includes(:post) }, :class_name => "VerySpecialComment"
55 56
   has_many :special_comments
56  
-  has_many :nonexistant_comments, :class_name => 'Comment', :conditions => 'comments.id < 0'
  57
+  has_many :nonexistant_comments, -> { where 'comments.id < 0' }, :class_name => 'Comment'
57 58
 
58 59
   has_many :special_comments_ratings, :through => :special_comments, :source => :ratings
59 60
   has_many :special_comments_ratings_taggings, :through => :special_comments_ratings, :source => :taggings
@@ -69,7 +70,7 @@ def add_joins_and_select
69 70
     end
70 71
   end
71 72
 
72  
-  has_many :interpolated_taggings, :class_name => 'Tagging', :as => :taggable, :conditions => proc { "1 = #{1}" }
  73
+  has_many :interpolated_taggings, -> { where "1 = #{1}" }, :class_name => 'Tagging', :as => :taggable
73 74
   has_many :interpolated_tags, :through => :taggings
74 75
   has_many :interpolated_tags_2, :through => :interpolated_taggings, :source => :tag
75 76
 
@@ -79,18 +80,18 @@ def add_joins_and_select
79 80
   has_many :tags_with_destroy, :through => :taggings, :source => :tag, :dependent => :destroy
80 81
   has_many :tags_with_nullify, :through => :taggings, :source => :tag, :dependent => :nullify
81 82
 
82  
-  has_many :misc_tags, :through => :taggings, :source => :tag, :conditions => { :tags => { :name => 'Misc' } }
  83
+  has_many :misc_tags, -> { where :tags => { :name => 'Misc' } }, :through => :taggings, :source => :tag
83 84
   has_many :funky_tags, :through => :taggings, :source => :tag
84 85
   has_many :super_tags, :through => :taggings
85 86
   has_many :tags_with_primary_key, :through => :taggings, :source => :tag_with_primary_key
86 87
   has_one :tagging, :as => :taggable
87 88
 
88  
-  has_many :first_taggings, :as => :taggable, :class_name => 'Tagging', :conditions => { :taggings => { :comment => 'first' } }
89  
-  has_many :first_blue_tags, :through => :first_taggings, :source => :tag, :conditions => { :tags => { :name => 'Blue' } }
  89
+  has_many :first_taggings, -> { where :taggings => { :comment => 'first' } }, :as => :taggable, :class_name => 'Tagging'
  90
+  has_many :first_blue_tags, -> { where :tags => { :name => 'Blue' } }, :through => :first_taggings, :source => :tag
90 91
 
91  
-  has_many :first_blue_tags_2, :through => :taggings, :source => :blue_tag, :conditions => { :taggings => { :comment => 'first' } }
  92
+  has_many :first_blue_tags_2, -> { where :taggings => { :comment => 'first' } }, :through => :taggings, :source => :blue_tag
92 93
 
93  
-  has_many :invalid_taggings, :as => :taggable, :class_name => "Tagging", :conditions => 'taggings.id < 0'
  94
+  has_many :invalid_taggings, -> { where 'taggings.id < 0' }, :as => :taggable, :class_name => "Tagging"
94 95
   has_many :invalid_tags, :through => :invalid_taggings, :source => :tag
95 96
 
96 97
   has_many :categorizations, :foreign_key => :category_id
@@ -109,7 +110,7 @@ def add_joins_and_select
109 110
 
110 111
   has_many :readers
111 112
   has_many :secure_readers
112  
-  has_many :readers_with_person, :include => :person, :class_name => "Reader"
  113
+  has_many :readers_with_person, -> { includes(:person) }, :class_name => "Reader"
113 114
   has_many :people, :through => :readers
114 115
   has_many :secure_people, :through => :secure_readers
115 116
   has_many :single_people, :through => :readers
@@ -118,7 +119,7 @@ def add_joins_and_select
118 119
               :after_add     => lambda {|owner, reader| log(:added,   :after,  reader.first_name) },
119 120
               :before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) },
120 121
               :after_remove  => lambda {|owner, reader| log(:removed, :after,  reader.first_name) }
121  
-  has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true }
  122
+  has_many :skimmers, -> { where :skimmer => true }, :class_name => 'Reader'
122 123
   has_many :impatient_people, :through => :skimmers, :source => :person
123 124
 
124 125
   def self.top(limit)
18  activerecord/test/models/project.rb
... ...
@@ -1,12 +1,12 @@
1 1
 class Project < ActiveRecord::Base
2  
-  has_and_belongs_to_many :developers, :uniq => true, :order => 'developers.name desc, developers.id desc'
3  
-  has_and_belongs_to_many :readonly_developers, :class_name => "Developer", :readonly => true
4  
-  has_and_belongs_to_many :selected_developers, :class_name => "Developer", :select => "developers.*", :uniq => true
5  
-  has_and_belongs_to_many :non_unique_developers, :order => 'developers.name desc, developers.id desc', :class_name => 'Developer'
6  
-  has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1
7  
-  has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true
8  
-  has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true
9  
-  has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0"
  2
+  has_and_belongs_to_many :developers, -> { uniq.order 'developers.name desc, developers.id desc' }
  3
+  has_and_belongs_to_many :readonly_developers, -> { readonly }, :class_name => "Developer"
  4
+  has_and_belongs_to_many :selected_developers, -> { uniq.select "developers.*" }, :class_name => "Developer"
  5
+  has_and_belongs_to_many :non_unique_developers, -> { order 'developers.name desc, developers.id desc' }, :class_name => 'Developer'
  6
+  has_and_belongs_to_many :limited_developers, -> { limit 1 }, :class_name => "Developer"
  7
+  has_and_belongs_to_many :developers_named_david, -> { where("name = 'David'").uniq }, :class_name => "Developer"
  8
+  has_and_belongs_to_many :developers_named_david_with_hash_conditions, -> { where(:name => 'David').uniq }, :class_name => "Developer"
  9
+  has_and_belongs_to_many :salaried_developers, -> { where "salary > 0" }, :class_name => "Developer"
10 10
   has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => proc { "SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id" }
11 11
   has_and_belongs_to_many :developers_with_multiline_finder_sql, :class_name => "Developer", :finder_sql => proc {
12 12
     "SELECT
@@ -20,7 +20,7 @@ class Project < ActiveRecord::Base
20 20
                             :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"},
21 21
                             :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"},
22 22
                             :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"}
23  
-  has_and_belongs_to_many :well_payed_salary_groups, :class_name => "Developer", :group => "developers.salary", :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary"
  23
+  has_and_belongs_to_many :well_payed_salary_groups, -> { group("developers.salary").having("SUM(salary) > 10000").select("SUM(salary) as salary") }, :class_name => "Developer"
24 24
 
25 25
   attr_accessor :developers_log
26 26
   after_initialize :set_developers_log
4  activerecord/test/models/sponsor.rb
@@ -2,6 +2,6 @@ class Sponsor < ActiveRecord::Base
2 2
   belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id"
3 3
   belongs_to :sponsorable, :polymorphic => true
4 4
   belongs_to :thing, :polymorphic => true, :foreign_type => :sponsorable_type, :foreign_key => :sponsorable_id
5  
-  belongs_to :sponsorable_with_conditions, :polymorphic => true,
6  
-             :foreign_type => 'sponsorable_type', :foreign_key => 'sponsorable_id', :conditions => {:name => 'Ernie'}
  5
+  belongs_to :sponsorable_with_conditions, -> { where :name => 'Ernie'}, :polymorphic => true,
  6
+             :foreign_type => 'sponsorable_type', :foreign_key => 'sponsorable_id'
7 7
 end
6  activerecord/test/models/tagging.rb
@@ -3,12 +3,12 @@ module Taggable
3 3
 end
4 4
 
5 5
 class Tagging < ActiveRecord::Base
6  
-  belongs_to :tag, :include => :tagging
  6
+  belongs_to :tag, -> { includes(:tagging) }
7 7
   belongs_to :super_tag,   :class_name => 'Tag', :foreign_key => 'super_tag_id'
8 8
   belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id'
9  
-  belongs_to :blue_tag,    :class_name => 'Tag', :foreign_key => :tag_id, :conditions => { :tags => { :name => 'Blue' } }
  9
+  belongs_to :blue_tag, -> { where :tags => { :name => 'Blue' } }, :class_name => 'Tag', :foreign_key => :tag_id
10 10
   belongs_to :tag_with_primary_key, :class_name => 'Tag', :foreign_key => :tag_id, :primary_key => :custom_primary_key
11  
-  belongs_to :interpolated_tag, :class_name => 'Tag', :foreign_key => :tag_id, :conditions => proc { "1 = #{1}" }
  11
+  belongs_to :interpolated_tag, -> { where "1 = #{1}" }, :class_name => 'Tag', :foreign_key => :tag_id
12 12
   belongs_to :taggable, :polymorphic => true, :counter_cache => true
13 13
   has_many :things, :through => :taggable
14 14
 end

10 notes on commit e1cfa6e

Bogdan Gusiev

Looks a little crazy. Why this is impossible?

has_many :posts_with_comments do
  includes(:comments)
end
Rafael Mendonça França
Owner

@bogdan an association with block define an association extension.

Bogdan Gusiev

Yep, I thought they are deprected as well as :extend option as they are kinda similar functionality. Don't you think that deprecating this type of extension is also a good idea?

José Valim
Owner

@bogdan I don't think we should deprecate :extend but I believe scoping an association will be much more common than extending it, so I prefer a syntax similar to what you proposed:

has_many(:posts_with_comments) { |s| s.includes(:comments) }

Or:

has_many :posts_with_comments do |s|
  s.includes(:comments)
end

Note that I explicitly expect the scope. Is there any reason why we don't pass the scope to the block instead of relying on instance_exec/instance_eval and friends?

Such syntax should be backwards compatible (because we check the block arity) and cleaner (without instance_exec/eval).

Wdyt? /cc @jonleighton

Bogdan Gusiev

@josevalim not sure I am following you: are you saying that :extend is not deprecated?

commit ac4d101
Author: Jon Leighton j@jonathanleighton.com
Date: Fri Jul 13 18:55:38 2012 +0100

move the deprecated options into active_record_deprecated_finders
José Valim
Owner

@bogdan it should be undeprecated. All the options that are not actually finders need to be undeprecated.

José Valim
Owner

@bogdan it should be undeprecated. All the options that are not actually finders need to be undeprecated.

Actually I was wrong. @jonleighton said it can be achieved with relation.extending(...).

Bogdan Gusiev

@josevalim Ok I see.

As the for syntax you propose.
Don't pass scope explicitly is kinda Rails style and it's more about general policy in such block API.
As I remember create_table switched to instance eval and friends not far time ago.

In case we want to introduce the new syntax, in order to keep backward compatibility as @rafaelfranca said, I see the only one way to do it:

has_many :posts_with_comments {|s| s.includes(:comments) }

as you proposed
And (OPTIONAL) deprecate the following

has_many :posts_with_extension do
  def approve_all
    ...
  end
end 

With suggestion to do (as this is pretty rare use case):

has_many :posts_with_extension do |s|
   s.extending(ApprovablePosts)
end
module ApprovablePosts
  def approve_all
  end
end
José Valim
Owner

Don't pass scope explicitly is kinda Rails style and it's more about general policy in such block API.

That's not true. We didn't use instance_eval until Rails 3 and the only exception when Rails 3 came out was the routes API.

Jon Leighton
Owner

@bogdan I've had a chat with @josevalim about this just now. the core team will continue to consider this new API and depending on the thoughts of the team, it could well change. there are various options, each with pros and cons and different proponents. so don't consider it set in stone until rails 4 actually ships :)

Please sign in to comment.
Something went wrong with that request. Please try again.