Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add magic encoding comment to generated files

#640

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: #42 PostgreSQL: Use all config options in rake db:create and db:test:purgeS22Unknown 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!

Reported by F2Andy · July 17th, 2008 @ 11:21 AM

State: resolved
Milestone: 2.x
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. Glenn Powell
    Glenn Powell

    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.

    August 21st, 2008 @ 10:27 AM

  2. Glenn Powell
    Glenn Powell

    woops...

    
    class Comment < ActiveRecord::Base
      belongs_to :post
    end
    

    August 21st, 2008 @ 10:29 AM

  3. Adrian Mugnolo
    Adrian Mugnolo

    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.

    August 23rd, 2008 @ 03:09 PM

  4. Tarmo Tänav
    Tarmo Tänav

    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?

    August 23rd, 2008 @ 07:01 PM

  5. Adrian Mugnolo
    Adrian Mugnolo

    +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.

    August 23rd, 2008 @ 07:13 PM

  6. Repository
    Repository
    • State changed from new to resolved

    (from [9223a919111867e6b47b2627c30d8eb21e8ac7d9]) Generate belongs_to associations automatically for 'references' types [#640 Add magic encoding comment to generated files state:resolved] http://github.com/rails/rails/co...

    August 23rd, 2008 @ 08:01 PM

  7. Ryan Bigg
    Ryan Bigg
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:14 PM

  8. Jeff Kreeftmeijer
  9. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 21st, 2010 @ 03:38 AM

  10. Jeff Kreeftmeijer
  11. Jeff Kreeftmeijer
  12. bingbing