# Can I get some help with my code here?

**URL:** <https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144>\
**Category:** General\
**Created:** [April 9, 2017, 3:17am UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144 "2017-04-09T03:17:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![barjit](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ruby.org.au/barjit/32/110_2.png) [@barjit](https://forum.ruby.org.au/u/barjit)\
**Post date:** [April 9, 2017, 3:17am UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144/1 "2017-04-09T03:17:33Z")

</div>

Hi,

Not sure if this is the appropriate place to put this but I asked a question on Stack Overflow and got a bit of interest but now my question seems to have been buried. Just wondering if I could get some help through here?

Here’s a link to my question: [http://stackoverflow.com/questions/43229523/rails-why-is-my-parent-object-not-saving](http://stackoverflow.com/questions/43229523/rails-why-is-my-parent-object-not-saving)

So the _items_ have a shopping\_list\_id attached but the _shopping-list_ item itself won’t save for some reason.

Please let me know if there’s a more appropriate place to put this.

Thanks,

Jeff  
🙂

---

<div class="post-metadata">

**Author:** ![antulik](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ruby.org.au/antulik/32/2_2.png) [@antulik](https://forum.ruby.org.au/u/antulik)\
**Post date:** [April 10, 2017, 1:09am UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144/2 "2017-04-10T01:09:06Z")

</div>

Will reply here and if works repost to SO.

#1  
As per suggested solution you need to change line

```ruby
@shopping_list = ShoppingList.new(shopping_list_params)

```

to

```ruby
@shopping_list = ShoppingList.create(shopping_list_params)

```

#2  
Also, update the line

```ruby
@shopping_list.items.build(name: ingredient.name, quantity: ingredient.quantity, unit: ingredient.unit, shopping_list_id: @shopping_list.id)

```

to (remove `shopping_list_id` param as it will be automatically set for you)

```ruby
@shopping_list.items.build(name: ingredient.name, quantity: ingredient.quantity, unit: ingredient.unit)

```

If that doesn’t work maybe you could upload the project so it’s easier to debug

---

<div class="post-metadata">

**Author:** ![barjit](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ruby.org.au/barjit/32/110_2.png) [@barjit](https://forum.ruby.org.au/u/barjit)\
**Post date:** [April 10, 2017, 2:11am UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144/3 "2017-04-10T02:11:54Z")

</div>

Ahh, thank you! - I will try this out tonight.

---

<div class="post-metadata">

**Author:** ![barjit](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ruby.org.au/barjit/32/110_2.png) [@barjit](https://forum.ruby.org.au/u/barjit)\
**Post date:** [April 10, 2017, 8:40am UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144/4 "2017-04-10T08:40:20Z")

</div>

Hi Anton,

Unfortunately it didn’t work, when I use _create_ instead of _new_, the same error gets hung up there as opposed to in the ‘respond\_to’ block.

I really don’t get why the shopping\_list controller is taking issue with the shopping\_list\_id. Wouldn’t its id parameter name just be, _id_ instead of shopping\_list\_id? My items table does have a shopping\_list\_id column name, would that have anything to do with it?

Here’s a link to the repo if you want to have a look!

> **[barjit/shop\_right](https://github.com/barjit/shop_right)**
>
> Users can choose which meals they'd like to eat for the week and the ingredients for those meals will be added to a shopping list. - barjit/shop\_right

I’ve edited the shopping\_lists controller to reflect the above changes.

Thanks,

Jeff

---

<div class="post-metadata">

**Author:** ![antulik](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ruby.org.au/antulik/32/2_2.png) [@antulik](https://forum.ruby.org.au/u/antulik)\
**Post date:** [April 10, 2017, 11:08pm UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144/5 "2017-04-10T23:08:28Z")

</div>

The issue is that `Meal` model has `belongs_to :shopping_list` but it doesn’t have column `shopping_list_id`.

So when you run

```ruby
ShoppingList.create(meal_ids: ['1'])

```

Rails tries to create `Meal` model and link it back to the `ShoppingList`, but it can’t since there is no such attribute and error tells you that `ActiveModel::MissingAttributeError`. To fix create a migration e.g.:

```ruby
add_column :meals, :shopping_list_id, :integer

```

---

<div class="post-metadata">

**Author:** ![barjit](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ruby.org.au/barjit/32/110_2.png) [@barjit](https://forum.ruby.org.au/u/barjit)\
**Post date:** [April 11, 2017, 7:33am UTC](https://forum.ruby.org.au/t/can-i-get-some-help-with-my-code-here/144/6 "2017-04-11T07:33:33Z")

</div>

Thank you very much @antulik, this worked!
