Reputation: 1481
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"
})
Upvotes: 0
Views: 50
Reputation: 36880
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
Upvotes: 1