diff options
author | Tarmo Tänav <tarmo@itech.ee> | 2008-08-23 20:58:44 +0300 |
---|---|---|
committer | Tarmo Tänav <tarmo@itech.ee> | 2008-08-23 20:58:44 +0300 |
commit | 9223a919111867e6b47b2627c30d8eb21e8ac7d9 (patch) | |
tree | 8c1077287506f22752989c1716cb6cd19871c825 | |
parent | a652c300ac9d60f6420d1cf86632f6a3c4ceef17 (diff) | |
download | rails-9223a919111867e6b47b2627c30d8eb21e8ac7d9.tar.gz rails-9223a919111867e6b47b2627c30d8eb21e8ac7d9.tar.bz2 rails-9223a919111867e6b47b2627c30d8eb21e8ac7d9.zip |
Generate belongs_to associations automatically for 'references' types [#640 state:resolved]
-rw-r--r-- | railties/lib/rails_generator/generators/components/model/templates/model.rb | 3 | ||||
-rw-r--r-- | railties/test/generators/rails_model_generator_test.rb | 8 |
2 files changed, 11 insertions, 0 deletions
diff --git a/railties/lib/rails_generator/generators/components/model/templates/model.rb b/railties/lib/rails_generator/generators/components/model/templates/model.rb index 8d4c89e912..8bf93b9e8b 100644 --- a/railties/lib/rails_generator/generators/components/model/templates/model.rb +++ b/railties/lib/rails_generator/generators/components/model/templates/model.rb @@ -1,2 +1,5 @@ class <%= class_name %> < ActiveRecord::Base +<% attributes.select { |a| a.type.to_s == 'references' }.each do |attribute| -%> + belongs_to :<%= attribute.name %> +<% end -%> end diff --git a/railties/test/generators/rails_model_generator_test.rb b/railties/test/generators/rails_model_generator_test.rb index 0bfc338566..67cd3aa26f 100644 --- a/railties/test/generators/rails_model_generator_test.rb +++ b/railties/test/generators/rails_model_generator_test.rb @@ -29,4 +29,12 @@ class RailsModelGeneratorTest < GeneratorTestCase assert_generated_column t, :created_at, :timestamp end end + + def test_model_with_reference_attributes_generates_belongs_to_associations + run_generator('model', %w(Product name:string supplier:references)) + + assert_generated_model_for :product do |body| + assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" + end + end end |