I have been trying for hours to create Items that belongs_to a Booth. I have declared the following:
class Item < ActiveRecord::Base
belongs_to :booth
belongs_to :category
end
and
class Booth < ActiveRecord::Base
belongs_to :user
has_many :items
validates :user_id, presence: true
validates :name, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 25}
end
According to the ror documentation, this build method should work since the item belongs_to the booth:
class ItemsController < ApplicationController
def create
@item = booth.build_item(item_params)
if @item.save
flash[:notice] = "Successfully created product."
redirect_to @item
else
render :action => 'new'
end
end
However the error message says that booth is an undefined variable or method. Shouldn't the association have defined the method? I've also tried booth.items.build and many other versions all of which have failed to recognize the association. I think I'm not fundamentally understanding this even after reading the docs many times. Can someone please help? Thanks a lot.
Assuming this method is in a controller, the problem is that
boothisn't defined anywhere. It looks like this is in ItemsController (your question could be a lot more specific in this regard), so what isboothsupposed to be?Your code seems to assume that
boothis an instance of Booth, in which casebooth.build_itemwould initialize an Item object associated with it, but it doesn't work because you've never assigned anything tobooth.Where your confusion lies, I think, is in the difference between models and controllers. For example, if you had this in your Item model:
...both (1) and (2) would work, because in both cases
boothis the instance methodItem#booth, which is defined by thebelongs_to :boothassociation.But ItemsController doesn't know much about Booth, and there is no
ItemsController#boothmethod. ItemsController doesn't even know much about Item.This time, (1) raises an error because
boothisn't a method that exists in ItemsController. (2) will work, however, because the instance of Item has aboothmethod. (3) also works, because we've retrieved the Booth from the database and assigned it to the variablebooth.(I'm assuming, in both of these examples, that an Item with
id123 exists, and that it's associated with a Booth withid456.)