How do I convert a SQL query to rails?

Hi,

I’m new to ROR. The query I created below is functioning properly. I’m trying to prepare an interrogation. But I didn’t succeed. The query is as follows;
@rad_check.tenant_id = SELECT groupname FROM radgrs INNER JOIN nas WHERE radgrs.tenant_id = nas.tenant_id AND nas.realipaddr = “192.168.22.175”

How do I make a switch case for the following queries in another question? Returns NULL if the query fails.

Thank you for caring.

  def realipaddr
    request.remote_addr
  end

  def create
    @rad_check = RadCheck.new(rad_check_params)
    @rad_check.tenant_id = Na.find_by(realipaddr: realipaddr, active: :true).tenant_id

    respond_to do |format|
      if @rad_check.save
        format.html { redirect_to @rad_check, notice: 'Rad check was successfully created.' }
        format.json { render :show, status: :created, location: @rad_check }
      else
        format.html { render :new }
        format.json { render json: @rad_check.errors, status: :unprocessable_entity }
      end
    end
  end

rails way would be something like this:

def create
  tenant = Na.find_by!(realipaddr: realipaddr, active: :true) # this line will return Na or returns 404 (if default behaviour for ActiveRecord::RecordNotFound)
  tenant.rad_checks.new(rad_check_params) # with proper setup in tenant model (has_many :rad_checks) it should set tenant_id properly and it's easier to read ("I'm initialising new RadCheck object in scope of the tenant")

  # the rest is fine more or less fine
  respond_to do |format|
    if @rad_check.save
      format.html { redirect_to @rad_check, notice: 'Rad check was successfully created.' }
      format.json { render :show, status: :created, location: @rad_check }
    else
      format.html { render :new }
      format.json { render json: @rad_check.errors, status: :unprocessable_entity }
    end
  end
end