Jay McNaught
Jay McNaught

Reputation: 31

How can I create a belongs_to model with the relative has_many id?

    @story = Story.create(story_params, listing_id: @listing.id)

Story belongs to listing, I'm trying to have it so upon creation the listing id is passed in so the story is attached to that listing. However, I am getting the following error upon trying to use this.

wrong number of arguments (2 for 1)

on line:

  @story = Story.create(story_params, listing_id: @listing.id)

How should I be doing this?

Upvotes: 0

Views: 29

Answers (1)

Agis
Agis

Reputation: 33626

Like this:

@story = @listing.stories.create(story_params)

Upvotes: 1

Related Questions