Home > Enterprise >  RAILS AS LISTED ONLY IDS SAME AS CURRENT USER
RAILS AS LISTED ONLY IDS SAME AS CURRENT USER

Time:06-21

Good afternoon. I'm new to Rails and I'm using Google Translate to post here. I have these 3 tables, and their relationships:

table: ata_publics
- ata_public_id
- provider_id
table: ride_requests
- ride_request_id
- hitchhiker_id
table: payments
payment_id
ride_request_id

The relationship is like this: Model: payment

belongs_to :ride_request

Model: ride_request

belongs_to :ata_public
has_one :payment
belongs_to :hitchhiker, class_name: "User"

Model: ata_public

has_many :ride_request
has_one :payment
belongs_to :provider, class_name: "User"

my question is: I want to list all payments that have the same provider_id as the current user.

If I try to do something similar in Payments: self.ride_request.ata_public.provider_id to get the id of the provider, gives error: NoMethodError: undefined method `ride_request'

I don't know if it got confusing, but when I list all the payments, they all come, I want only the payments that are from the provider_id equal to the current user.

  def index
    @payments = Payment.all
    
    render json: Paginate.new(
      pagination_params[:page],
      pagination_params[:limit]
    ).call(@payments), status: 200

[![imagem_table]: https://i.stack.imgur.com/EIzx2.png

CodePudding user response:

Changing

@payments = Payment.all

to

@payments = Payment
  .joins(ride_request: :payment)
  .where(payments: { provider_id: current_user.id })

should work.

  • Related