Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3798206
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:32:04+00:00 2026-05-19T13:32:04+00:00

I have two models: class Thread < ActiveRecord::Base has_many :attachments end class Attachment <

  • 0

I have two models:

class Thread < ActiveRecord::Base
  has_many :attachments
end

class Attachment < ActiveRecord::Base
  belongs_to :thread
end

When creating a new thread I want to build attachments before saving. I’m doing:

@thread.attachments.build(...)

but that is erroring with: failed with NoMethodError: undefined method `build' for #<Class:0x10338c828

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:32:05+00:00Added an answer on May 19, 2026 at 1:32 pm

    You should be able to use @thread.attachments.build to create a new unsaved attachment.

    If you have :autosave => true on your has_many (either explicitly or via accepts_nested_attributes_for) they will be saved when the parent thread saves.

    You may need to set :inverse_of on the associations for saving of new records to work correctly:

    class Thread < ActiveRecord::Base
      has_many :attachments, :inverse_of => :thread, :autosave => true
    end
    
    class Attachment < ActiveRecord::Base
      belongs_to :thread, :inverse_of => :attachments
    end
    

    As mentioned by @zetetic: Thread is a built in Ruby class and you should not use the same name as a system class in your app.

    There are a number of classes in any language which you learn to not use. In such cases I would add something to my classname to make it more descriptive, assuming there’s not a better name altogether. Taking a stab at what you app is, you should rename this from Thread to something like Message or MessageThread.

    What’s happening is that as Thread is a core class that is loaded before your app code. There’s already a Thread class in memory and Rails does not go looking for it in development mode when you first reference it. Rails loads your classes on demand in development by looking for a thread.rb file when Thread is referenced and not currently loaded.

    In production mode, Rails loads all classes at startup and the differing base class (Object vs ActiveRecord::Base) will result in a TypeError.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Rails. I have two models, Person and Day. class Person <
Let's say I have two models, Classes and People. A Class might have one
I have two models, Article and Post that both inherit from a base model
I have these two models: class Application_Model_List extends Zend_Db_Table_Abstract { protected $_name = 'list';
Say I have these two models: public class City extends Model { @ManyToOne private
I have two models. We'll call them object A and object B. Their design
I have two models indexed for searching (User and Item). I'm trying to do
I have two models, Room and Image . Image is a generic model that
I have two models: Person and Relation. The second one stores information about relations
Suppose you have two models, User and City, joined by a third model CityPermission:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.