I have tried and tried to find the answer elsewhere, but I just cannot resolve this problem.
I have a model call BidSignal that is related to a model called BuySignal. A BuySignal has many BidSignals and a BidSignal belongs to a BuySignal.
When I create a new BidSignal using the new method in the BidSignal controller I want to capture the BuySignal that it relates to, so I can save the relation in the BidSignal create method. I'm currently doing this by passing the buysignal_id to BidSignal controller on a call to the new method. But when I come to create the BidSignal record and try to recall the buysignal_id to find the BuySignal record, I get an error - Couldn't find Buysignal without an ID
Bid Signal New Method
def new
@buy_signal_idx = params[:buysignal_id]
@bid_signal = BidSignal.new
end
Bid Signal Create Method
def create
@a_buysignal = Buysignal.all
@a_buysignal = @a_buysignal.find(@buy_signal_idx)
@a_bid_signal = @a_buysignal.bid_signals.build(bid_signal_params)
if @a_bid_signal.save
flash[:success] = "Bid Signal Successfully Created!"
redirect_to @a_bid_signal
else
render action: 'new'
end
end
However, if I replace: @a_buysignal = @a_buysignal.find(@buy_signal_idx)
with:
@a_buysignal = @a_buysignal.find(300)
It all works fine ( except of course all my bid signals are being related to buy signal with id 300 ).
It's as if the @buy_signal_idx is not accessible within the create method. I have tried abstracting this out so that new calls a class to set a variable for the bid signal through it's own new method and then use a getter to return the buy_signal_idx in the bid signal create method. But i get the same result.
I think I get that if I initiate an instance variable in a method that variable should be accessible by other methods in that object instance - but it's not working.
I know i'm being stupid and missing something fundamental - but what is it ?