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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:32:55+00:00 2026-05-26T01:32:55+00:00

I have a User model, an Event Model, an Event Priority Model and a

  • 0

I have a User model, an Event Model, an Event Priority Model and a Event Type Model. Model code as follows:

class Event < ActiveRecord::Base

  belongs_to :user
  belongs_to :event_priority
  belongs_to :event_type

  attr_accessible :name, :raised_date, :location, :description, :longtitude, :latitude
  attr_protected :raised_user_id, :event_priority_id, :event_type_id
end

class EventPriority < ActiveRecord::Base

  has_many :events
  has_many :users, :through => :events
  has_many :event_types, :through => :events

end

class EventType < ActiveRecord::Base

  has_many :events
  has_many :users, :through => :events
  has_many :event_priorities, :through => :events

end

class User < ActiveRecord::Base

  attr_accessor :password
  attr_accessible :first_name, :last_name, :full_name, :password, password_confirmation

  has_many :events, :foreign_key => "raised_user_id", :dependent => :destroy
  has_many :event_priorities, :through => :events
  has_many :event_types, :through => :events

end

Can anyone explain the inability to get from the Event back to the User in the following rails console example?

irb(main):027:0> @user = User.find(2)
=> Returns the user with an ID of 2.

irb(main):028:0> @user.events
=> Returns all events for that user.

irb(main):029:0> @user.events.first.user
=> nil --HUH????

irb(main):031:0> @event = @user.events.first
=> Saves and returns the first event created by the user.

irb(main):032:0> @event.user
=> nil --Again, WHY??

irb(main):033:0> @events = Event.all
=> Saves and returns all events.

irb(main):035:0> @events.first.user
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.first
        from (irb):35
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activemodel-3.0.
6/lib/active_model/attribute_methods.rb:279 -- AGAIN, WHY?
  • 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-26T01:32:55+00:00Added an answer on May 26, 2026 at 1:32 am

    Do you have a good reason for not just using user_id as the foreign key in the events table? (that’s what is causing your problem)

    Try adding the foreign_key option in the Event class as well

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

Sidebar

Related Questions

I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
Let's say I have such model class Event(models.Model) users_count = models.IntegerField(default=0) users = models.ManyToManyField(User)
I have a django model roughly as shown below: class Event(db.Model): creator = db.ReferenceProperty(User,
I have an Event model, which stores an event feed for each user. I
Say I have a User model, a Task model that belongs_to :user , :has_one
I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model
I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model
I have a model for Event that I would like to have a user
First here's my current setup: models/user.rb class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable,
I have a store as follows. var eventsStore = new Ext.data.Store({ model: 'Event', sorters:

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.