I have what I think is a simple structure:
class GroupEvent < ActiveRecord::Base
attr_accessible :user_id, :title, :description, :identifier, :start_date, :end_date, :time_zone, :url, :capacity, :info_url, :logo_url, :logo_ssl_url, :status
has_one :event_venue
has_one :event_organizer
has_one :event_ticket
has_one :event_condition
belongs_to :user
end
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :group_events
...
end
But in the Rails console when I execute
@user = User.new
@event = @user.group_events.build
I get an error: ActiveRecord::InverseOfAssociationNotFoundError: Could not find the inverse association for group_events (:user in GroupEvent).
Update: I still can’t get the user and the GroupEvent to see each other. GroupEvent.new and EventVenue.new create new objects, and
@event = GroupEvent.new
@venue = @event.build_event_venue
creates an @event.event_venue object, as it should. (Though I’m still confused that
@event.event_venue.build
returns a NoMethodError: undefined method `build’ for nil:NilClass.)
Update: It works now.
creates a new GroupEvent object. For anyone coming after, I apologize, I don’t know what I did. The above code all still looks the same. I wasn’t aware that I needed to restart the console each time I made a change, but restarting the console seemed to fix my problems.
A lingering question is bothering me. Both @user.build_group_event and @user.group_event.build generate the same result (a GroupEvent object), but for my other classes, @event.build_event_venue generates an object, but @event.event_venue.build still returns a “NoMethodError: undefined method `build’ for nil:NilClass”. I just can’t figure it out. But I have working code, so I’m not going to make too big a deal about it.
Thank you for your help.