I'm trying to get the transaction id associated with the current_user but rails shos this error below
error:
ActiveRecord::RecordNotFound (Couldn't find Transaction with id=92 [WHERE "transactions"."user_id" = 24])
iv'e tried use where and just the transaction and with a conditional comparing the transaction.user_id with the current_user.id but show error!
someone have any hint about this kind of issue?
model user
user has_many transactions
model transaction
transaction belongs to user
transaction controller
def new
@transaction = Transaction.new
end
def create
@transaction = Transaction.build_user
end
def show
@transaction = current_user.transactions.find(params[:id])
end
As many pointed in comments build_user
does indeed create a new User instance (without saving it). Furthermore I believe build_user
is not a class method but an instance method instead. Therefore Transaction.build_user should be raising 'undefined method' exception. Also, build_user returns a User instance, so even if you're code was @transaction = Transaction.new.build_user
@transaction would be a newly created User instance.
And again, what others already pointed out in comments, your transaction wouldn't necessarily be associated to the current_user
for you to fetch it like this in subsequent calls during the same session, rather, and supposing you properly built and saved the user, you would need to login as this 'new user' in order to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.