Skip to content

Commit

Permalink
Generate belongs_to associations automatically for 'references' types [
Browse files Browse the repository at this point in the history
…#640 state:resolved]
  • Loading branch information
tarmo committed Aug 23, 2008
1 parent a652c30 commit 9223a91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions railties/test/generators/rails_model_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ def test_model_with_attributes_generates_resources_with_attributes
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

0 comments on commit 9223a91

Please sign in to comment.