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 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
  • 1 View
  • 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

I have an observer which looks like this: class CommentObserver < ActiveRecord::Observer include ActionView::Helpers::UrlHelper
I have a NewRequest event handler (subscriber) in Pyramid which looks like this: @subscriber(NewRequest)
I have made a generic Observer interface and an Observable class, but can't compile
I have only one class with many instances. Every instance is observer of couple
I have a ViewController class that has a property that is the model which
I have make custom module in which I have make observer file. the observer
We all know the observer pattern : You have a subject which is able
I have observer.h , client.h and field.h files. In observer.h there is Subject class
In C++ methods can have optional arguments, like this: void myFunction (int arg1, int
I have a view with a ComboBox like this <ComboBox Name=cmbPurpose DisplayMemberPath=@value SelectedValuePath=@key ItemsSource={Binding

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.