0

Question: How can I get data which is associated with each other in my controller from separate controllers and models?

Issue: I need my @order.seller_id's (which matches a @user.id) and then i need the @user.stripe_token.

So i ultimately need the @user.stripe_token from the correct user_id, which is matched up through the @order.seller_id

The order has a seller_id which is a users id. This id is the same across all tables that have a user attached to it. So in order to transfer the money, I need the correct @user the seller_id is associated to

Here's the piece of code i need this for from my orderscontroller: (when a order gets updated, the charge gets captured and seller receives commission) It may not be set up 100% correctly atm but this is the currently where i'm at.

charge = Stripe::Charge.create({
    :amount      => (@order.order_price).to_i * 100,
    :description => 'Rails Stripe customer',
    :currency    => 'usd',
    :customer => @order.stripe_customer_token,

    })

transfer = Stripe::Transfer.create({
    application_fee_percent: 75
    :currency    => 'usd',
    destination: ---here's where i need the correct @user.stripe_token ---,
    transfer_group: "notes_328324"
    })

1 Answer 1

1

In the Order class you can do...

belongs_to :seller, foreign_key: :seller_id, class_name: 'User'

Once you've done this, rails knows about the relationship between Order and User, so you can then do...

@order.seller.stripe_token
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.