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

  • SEARCH
  • Home
  • 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 7433485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:40:42+00:00 2026-05-29T09:40:42+00:00

I have this code for creating a topic and post in a forum application

  • 0

I have this code for creating a topic and post in a forum application in Rails 3.1:

def create
  @topic = Topic.new(:name => params[:topic][:name], :last_post_at => Time.now)
  @topic.forum_id = params[:topic][:forum_id]
  @topic.user = current_user

  if @topic.save
    @post = Post.new(:content => params[:post][:content])
    @post.topic = @topic
    @post.user = current_user

    @post.save!
    ...

When posting to the create method via the corresponding form, the topic and the post are created and both save calls are successful.

When I call the create method via a functional test, the topic is saved but the post has validation errors.

ActiveRecord::RecordInvalid: Validation failed: 
app/controllers/topics_controller.rb:23:in `create'
test/functional/topics_controller_test.rb:26:in `block in <class:TopicsControllerTest>'

The test looks like this:

test "should create topic" do
  post :create, :topic => {:name => "New topic", :forum_id => forums(:one).id}, :post => {:content => "Post content"}
end

(current_user is logged in via a setup method.)

When I display the errors of the post object via the debugger or with @post.errors.full_messages, the error array is empty.

The Post model looks like this:

class Post < ActiveRecord::Base
  attr_accessible :content

  belongs_to :topic
  belongs_to :user
end

And the Topic model:

class Topic < ActiveRecord::Base
  belongs_to :user
  belongs_to :last_poster, class_name: 'User'

  attr_accessible :name, :last_poster_id, :last_post_at

  belongs_to :forum
  has_many :posts, :dependent => :destroy
end

How can I find out what is causing the validation error?

  • 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-29T09:40:43+00:00Added an answer on May 29, 2026 at 9:40 am

    The problem was that I used mocha’s Post.any_instance.stubs(:valid?).returns(false) in a test that was executed before my failing test.

    Apparently, you have to restore the original behavior before proceeding with other tests by calling Post.any_instance.unstub(:valid?).

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

Sidebar

Related Questions

I have this webdav code thats creating folders in SharePoint: HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(folderAddress);
I am creating a rails app and have used this code in one of
i have this code to read Excel file Dim scheduleFileName As String 'Creating Excel
I'm currently creating a Login form and have this code: string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
I've seen this Topic : Creating an instance from a class name and written
So I have this code creating a fake directory Options +FollowSymLinks RewriteEngine On RewriteCond
I have put together this code for creating a dynamic form unit Unit1; interface
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into

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.