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 6170543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:03:18+00:00 2026-05-23T23:03:18+00:00

So I have an observer which looks like this: class RecipeObserver < Mongoid::Observer def

  • 0

So I have an observer which looks like this:

class RecipeObserver < Mongoid::Observer

  def after_create(object)
    puts object.user
    puts "test"
  end

end

And so I have that working fine, but the relationship object.user is nil, like even though my Recipe class belongs_to a user, which has_many recipes as well. What am I doing wrong here?


Update

So here is my controller code and my model, if someone could please show me how a proper observer that creates another object works, that would be very helpful:

class RecipesController < ApplicationController
  respond_to :html
  before_filter :authenticate_user!, :only => :new

  # ... other actions

  def new
    @recipe = Recipe.new
    respond_with @recipe
  end

  def create
    @recipe = Recipe.new(params[:recipe])
    @recipe.save

    respond_with @recipe, :notice => "You've created a recipe!"
  end
end

My models (truncated for brevity):

class Recipe
  include Mongoid::Document

  # ... other methods and such

  belongs_to :user
end

class User
  include Mongoid::Document

  field :name

  # ... other methods and such

  has_many :recipes
end

So the big problem is that calling object.user in the after_create method is nil, but it doesn’t even throw an exception, just results in nothing being returned, which I find the most bizarre, I’m not sure really on how to start debugging this.

  • 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-23T23:03:19+00:00Added an answer on May 23, 2026 at 11:03 pm

    It doesn’t look like you’re actually assigning a user to the recipe anywhere. Try:

    def create
      @recipe = current_user.recipes.build(params[:recipe])
      @recipe.save
    
      respond_with @recipe, :notice => "You've created a recipe!"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class MyClass to which I want to add certain 'observer'
I have a class which depends on 3 classes, all 3 of which have
I have create a module which at this point does nothing but exist the
In C++ methods can have optional arguments, like this: void myFunction (int arg1, int
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications -
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
Are there any exemplary examples of the GoF Observer implemented in Python? I have
I have a class WebServiceCaller that uses NSURLConnection to make asynchronous calls to a
I have an application solution which is made up of a web app written

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.