dbateman3
dbateman3

Reputation: 11

Rails no method error associations

First time caller, long time listener. Im having an issue with my Rails associations. My goal is to make an event website where users can create events and attend events. I have created my models Users and Events and associations as such as:

class Event
  belongs_to :creator, :class_name => "User"
  ...
end

class User
  has_many :created_events, :foreign_key => "creator_id", :class_name => "Event", dependent: :destroy
  ...
end

My Event model has only a description and creator_id foreign key, where my user just has a name field. But I am getting a no method error when I try using @event = current_user.events.build. current_user just uses sessions to return the user id

I cannot for the life of me figure out why Im getting this error.

I have the full project on this Github repository.

Upvotes: 1

Views: 126

Answers (1)

jvillian
jvillian

Reputation: 20253

class User has_many :created_events, not has_many :events. So, try current_user.created_events.build instead of current_user.events.build.

Upvotes: 1

Related Questions