This project is archived and is in readonly mode.
Add magic encoding comment to generated files
Reported by F2Andy | July 17th, 2008 @ 11:21 AM | in 2.x
The references column type generates a column with _id appended to the name, so for example:
generate skaffold comment author:string body:text post:references
... will create a column called post_id of type integer. However, the fixture generated uses the column without the _id
This is with ails 2.1.0 and Ruby 1.8.6.
Recreating the bug
Create new project
rake database create all
generate skaffold post title:string body:text
generate skaffold comment author:string body:text post:references
migrate to current version
test
The error reported (with MySQL):
test_truth(CommentTest): ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column 'post' in 'field list': INSERT INTO `comments` (`updated_at`, `body`, `post`, `author`, `id`, `created_at`) VALUES ('2008-07-17 09:53:38', 'MyText', NULL, 'MyString', 996332877, '2008-07-17 09:53:38')
The migration generated
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.string :author
t.text :body
t.references :post
t.timestamps
end
end
def self.down
drop_table :comments
end
end
The fixture file generated
# Read about fixtures at http://ar.rubyonrails.org/classe...
one:
author: MyString
body: MyText
post:
two:
author: MyString
body: MyText
post:
The workaround
Add _id to the fixtures file in the right places!
Comments and changes to this ticket
-
Glenn Powell August 21st, 2008 @ 10:27 AM
I think your problem here is that you aren't creating your model correctly. The scaffold (not skaffold ??) generator does not add anything extra to your models/comment.rb file. So you need to explicitly add the reference to post:
class Comment < ActiveRecord::Base belongs_to :post end
This should allow the fixtures to work as they are generated.
-
Glenn Powell August 21st, 2008 @ 10:29 AM
woops...
class Comment < ActiveRecord::Base belongs_to :post end
-
Adrian Mugnolo August 23rd, 2008 @ 03:09 PM
Agree with Glenn. The generated fixtures are actually giving you a hint about the "foxy fixtures" feature. This allows using record label references instead of numeric ids on fixtures.
For example,
post_by_john: author: John Smith title: How to implement "lorem ipsum" body: > Lorem ipsum dolor sit amet, consectetur adipisicing elit.
comment_on_johns_post_by_mary: author: Mary White body: > That was a great post about using "lorem ipsum", John. post: post_by_john comment_on_johns_post_by_peter: author: Peter Black body: > Not sure, take a look at "dolor sit amet" instead. post: post_by_john
So, IMO the generated files are correct. You should be using more "post" and less "post_id" on fixtures.
-
Tarmo Tänav August 23rd, 2008 @ 07:01 PM
Indeed the expectation is that foxy fixtures be used, but perhaps the real fix here is automatically generating a model with a belongs_to for the "references" attributes?
-
Adrian Mugnolo August 23rd, 2008 @ 07:13 PM
+1 for Tarmo's idea. Still, I would stop there and don't try to check if the referenced model includes the matching has_many/has_one.
-
Repository August 23rd, 2008 @ 08:01 PM
- State changed from new to resolved
(from [9223a919111867e6b47b2627c30d8eb21e8ac7d9]) Generate belongs_to associations automatically for 'references' types [#640 state:resolved] http://github.com/rails/rails/co...
-
Ryan Bigg October 9th, 2010 @ 10:14 PM
- Importance changed from to Low
Automatic cleanup of spam.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 640 Table columns of type references given wrong name in fixtures (from [9223a919111867e6b47b2627c30d8eb21e8ac7d9]) Generat...